In Python 3.x and 2.x you can use use list
to force a copy of the keys to be made:
for i in list(d):
In Python 2.x calling keys
made a copy of the keys that you could iterate over while modifying the dict
:
for i in d.keys():
But note that in Python 3.x this second method doesn’t help with your error because keys
returns an a view object instead of copynig the keys into a list.