Back
I want to pop out all the large values and its keys in a dictionary and keep the smallest. Here is the part of my program
for key,value in dictionary.items(): for key1, value1 in dictionary.items(): if key1!= key and value > value1: dictionary.pop(key) print (dictionary)
for key,value in dictionary.items():
for key1, value1 in dictionary.items():
if key1!= key and value > value1:
dictionary.pop(key)
print (dictionary)
Which results in
RuntimeError: dictionary changed size during iteration
How can I avoid this error?
It looks like you are looking for the smallest value in the dictionary, so for that, you can do something like follows:
min(dictionary.values())
If you cannot use min, you can use sorted:
sorted(dictionary.values())[0]
31k questions
32.8k answers
501 comments
693 users