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 on Python 3.x, .keys
returns a view object instead, so it won’t fix your error.