Dictionary Changed Size During Iteration
How would I remedy the following error? for item in data: if data[item] is None: del data[item] RuntimeError: dictionary changed size during iteration It doesn't actu
Solution 1:
You have to move changing dictionary to another vairable:
changing_data = data
for item in data:
if changing_data[item] is None:
del changing_data[item]
data = changing_data
Solution 2:
This seems to be a matter of needing to change the item to be iterated over from the dictionary to the keys of the dictionary:
for key in data.keys():
if data[key] is None:
del data[key]
Now it doesn't complain about iterating over an item that has changed size during its iteration.
Post a Comment for "Dictionary Changed Size During Iteration"