Exam = {' Math ': ' n ', ' eng ': ' n ', ' CHN ': ' N ', ' phy ': ', ' Chem ': '}
Use the following traversal method to delete:
1. For e in exam:
2. If exam[e] = = ':
3. Del Exam[e]
The result is the following error, How to resolve:
Traceback (most recent call last):
File "untitled.py", line 3, in <module> to
e in exam:
runtimeerror: Dictionary changed size during iteration
Correct procedure:
1. S = {"1": A, "2": B, "3": C, "4":d, "5": E}
2. S_key = List (S.keys ())
3. For k_s in S_key:
4. #比如我要删除第四个元素
5.del s["4"]
Just in the For loop, which is equivalent to the operation of the linked list, it automatically calls the next method!
The dictionary iterator iterates through its keys, and in the process, you can't change the dictionary! Cannot delete, add data
To record the index of the element to be deleted, and then delete after the traversal, Exam.keys () returns a separate list
The above perfect solution Python traversal Delete the value of the element is empty in the dictionary the error problem is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.