0 votes
1 view
in Python by (47.7k points)

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)

Which results in

RuntimeError: dictionary changed size during iteration

How can I avoid this error?

1 Answer

0 votes
by (107k points)

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]

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...