Somehow unexpectedly encountered such a problem. During the for
loop in the dictionary, I need to delete entries from it, but how not to get the error:
RuntimeError: dictionary changed size during iteration
I even tried to do a dictionary snapshot before iteration and iterate the twin while removing it from the original.
Example:
>>> ot={12:'wqe',13:'wqe',14:'wqe',15:'wqe'} >>> ot1=ot >>> for i in ot1: ... del ot[i] ... Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: dictionary changed size during iteration
How to delete correctly?
upd: figured out
snapshot was appropriate, but made a little bit wrong like this:
>>> ot={12:'wqe',13:'wqe',14:'wqe',15:'wqe'} >>> ot1=ot.copy() >>> for i in ot1: ... del ot[i]