I want to optimize this code:
for MajorKey,v in enumerate(data):
for MinorKey,e in enumerate(v['events']):
if e['displayed'] == False:
del data[MajorKey]['events'][MinorKey]
The structure:
data : [{ //details,
"events" :[ { //more details,
"displayed" : True},
{ //more details,
"displayed" : False}
]
},
{ //...
}]
data
is a list. Each object in data has an 'events' array. Of those objects I want to delete those that have display == false
.
Don't enumerate, and don't delete (especially if you're iterating over the sequence you're deleting from). Slice-assign a list comprehension instead.
for v in data.itervalues():
v['events'][:] = [e for e in v['events'] if e['displayed']]