Modifying a Python dict while iterating over it
Solution 1:
Alex Martelli weighs in on this here.
It may not be safe to change the container (e.g. dict) while looping over the container.
So del d[f(k)]
may not be safe. As you know, the workaround is to use d.items()
(to loop over an independent copy of the container) instead of d.iteritems()
(which uses the same underlying container).
It is okay to modify the value at an existing index of the dict, but inserting values at new indices (e.g. d[g(k)]=v
) may not work.
Solution 2:
It is explicitly mentioned on the Python doc page (for Python 2.7) that
Using
iteritems()
while adding or deleting entries in the dictionary may raise aRuntimeError
or fail to iterate over all entries.
Similarly for Python 3.
The same holds for iter(d)
, d.iterkeys()
and d.itervalues()
, and I'll go as far as saying that it does for for k, v in d.items():
(I can't remember exactly what for
does, but I would not be surprised if the implementation called iter(d)
).
Solution 3:
You cannot do that, at least with d.iteritems()
. I tried it, and Python fails with
RuntimeError: dictionary changed size during iteration
If you instead use d.items()
, then it works.
In Python 3, d.items()
is a view into the dictionary, like d.iteritems()
in Python 2. To do this in Python 3, instead use d.copy().items()
. This will similarly allow us to iterate over a copy of the dictionary in order to avoid modifying the data structure we are iterating over.