How does python3.6 traverse and modify dictionaries

how does python3.6 traverse the modification dictionary, adding or deleting a dictionary in each nested dictionary, but it will report an error at run time? how can it not report an error?
A dictionary changed size during iteration exception will be thrown when traversing to modify the key-value pair of adding a dictionary.

Dec.27,2021

Let me give you a simple example:

>>> d={"a":10,"c":5}
>>> for k in d:
...     del d[k]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration

this problem can be solved by changing it to a key-value pair:

>>> for k,v in d.items():
...     del d[k]
...
>>> d
{}

therefore. You just need to modify it a little bit during the loop.

Menu