0

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
novastorm
  • 77
  • 7
  • 1
    See [this](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – alex Jan 02 '22 at 21:33
  • yeah I didn't even think about the problem with removing an item during iteration. I checked your link and it looks good, but the answer by @BrokenBenchmark is very concise – novastorm Jan 03 '22 at 06:34

1 Answers1

1

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'])
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 1
    Thanks for the reply! I haven't really used filter before, but I have checked documentation and it seems pretty straight forward. Thanks again – novastorm Jan 03 '22 at 06:31