I'm trying to remove some items of a dict based on their key, here is my code:
d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']
print l
The output will be:
[{'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}, {'a': 1}]
Is there a better way to do it?