I have a list of JSON responses that I want to parse before putting it into a DataFrame.
In my list of 15,000 responses I want to remove those that do not have a certain key in it.
What I have so far seems to be playing funny business with the looping after I delete an element and I'm not sure why.
If I run the below - it correctly finds 3 matches of the 15k that should be deleted.
Deleted! : 2591
Deleted! : 12306
Deleted! : 12307
-
try:
for i in range(len(trans)):
#print("checking for deletion: "+ str(i))
if 'CashBooks' not in trans[i]:
#del trans[i]
print("Deleted! : " + str(i))
except Exception as e:
print(str(e))
print('passed')
pass
However when I un-comment the del
I get errors like so:
Deleted! : 2591
Deleted! : 12305
list index out of range
passed
The list is quite large so it's hard to post sample data but hopefully someone can easily spot where I'm going wrong.
Thanks for your time.