The following small series will provide you with an article to solve the problem that an exception is reported when you delete an element during Python dictionary traversal. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
Error code ①
D = {'a': 1, 'B': 0, 'C': 1, 'D': 0} for key, val in d. items (): del (d [k])
Error code ② -- for Python3
D = {'a': 1, 'B': 0, 'C': 1, 'D': 0} for key, val in d. keys (): del (d [k])
Correct code
D = {'a': 1, 'B': 0, 'C': 1, 'D': 0} keys = list (d. keys () for key, val in keys: del (d [k])
The above article solves the problem that an exception is reported when deleting an element during Python dictionary traversal. I hope you can give me a reference and support PHP.
For more information about how to delete an element when retrieving a dictionary in Python, see PHP!