Home > OS >  Remove from dictionary seems to miss sometimes (python)
Remove from dictionary seems to miss sometimes (python)

Time:01-03

I have some json data in the following form:

data={'total': 5000, 'transactions': [{'info': foo, 'status': Success}, {'info': foo, 'status': Fail}]

I want to remove all the failures from the transactions, so I perform:

for txn in data['transactions']:
            if 'status' in txn:
                if txn['status'] == 'Fail':
                    data['transactions'].remove(txn)
                    data['total'] = data['total'] - 1
                    print('Fail 1 at: '   str(idx))

I believe this code works as it should (ie no errors and runs), but then I inspect the debugger and find failures still there. I even run this code immediately after:

for idx,txn in enumerate(data['transactions']):
     if 'status' in txn:
         if txn['status'] == 'Fail':
              print('Fail 2 at: ' str(idx))

Fail 2 returns less than Fail 1, but Fail 2 still returns more than zero. Am I missing something, like an incorrect removal here: data['transactions'].remove(txn) ?

See a small debug list of failures below:

Fail 1 at: 1
Fail 1 at: 81
Fail 1 at: 82
Fail 1 at: 105
Fail 1 at: 164

Fail 2 at: 82
Fail 2 at: 289
Fail 2 at: 353
Fail 2 at: 559
Fail 2 at: 573

CodePudding user response:

You're removing from the list while iterating. That can cause problems, as the memory is shifting while you're accessing it.

To resolve this issue, you can use filter():

data['transactions'] = list(filter(lambda x: x['status'] != 'Fail', data['transactions']))
print(data['transactions'])
  • Related