Skip to content Skip to sidebar Skip to footer

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 = datafor item indata:
  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 indata.keys():
    ifdata[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"