i desire pop out all the large values and also its keys in a dictionary, and also keep the smallest. Here is the component of mine program

for key,worth in dictionary.items(): because that key1, value1 in dictionary.items(): if key1!= essential and also value > value1: dictionary.pop(key) Print (dictionary) which outcomes in

RuntimeError: Dictionary readjusted size During iteratitop top just how have the right to ns prevent this error?


In Python3, Try

for key in list(dict.keys()): if condition: matchead del dict1 even more point have to be mindful as soon as loopinns a dict come update itns key:


keyPredeal with = ‘keyA’because that key, worth in Dict.items(): newkey = ‘/’.join() Dict = Dict.pop(key)Code2:

keyPrefix = ‘keyA’because that key, value in Dict.keys(): newvital = ‘/’.join() Dict = Dict.pop(key)outcome that code1/code2 is:

‘keyA/keyA/keyB’ : ”, ‘keyA/keyA/keyA’: ”mine way come resolve thins unmeant result:


alternative solutions

If you"re searching for ns smallest value in the Thesaurus friend deserve to execute this:

min(dictionary.values())If friend cannot use min, you deserve to usage sorted:


Why carry out i get this error?

on a next note, ns reason you"re experiencing a Runtins Error ins the in ns inside loons you change ns iterator her outer loons is based upon. As soon as friend pop one entry that ins however to be reached by the outer loop and also the outer iterator reachens it, it triens come accessibility a removed element, thus bring about the error.If girlfriend attempt come execute her password ~ above Python 2.7 (insteAD of 3.x) you"ltogether get, in fact, a vital Error.

You are watching: Runtimeerror: dictionary changed size during iteration

Wcap deserve to i perform to avoid the error?

If you want come change a iterable within a loop based upon itns iterator friend should usage a deep copy that it.


girlfriend deserve to usage copy.deepcopy come do a copy that ns Initial dict, loop over ns copy while adjust ns Initial one.

from copy import deepcopyd=dict()because that i in range(5): d=str(i)k=deepcopy(d)d<2>="22"print(k<2>)#the outcome will it is in 2.your trouble ins iterate end something the girlfriend are changing.


document the essential Throughout ns loons and also climate carry out dictionary.pop(key) once loop ins done. Favor this:

for key,value in dictionary.items(): because that key1, value1 in dictionary.items(): if key1!= essential and also value > value1: storedworth = essential dictionary.pop(key)
right here ins one way to deal with it:

native ns dictionary, acquire a list that keys, sorted by valueSince ns first crucial in this list has actually ns smallest value, you have the right to execute wcap you want with it.

See more: Sketches From Hedwig And The Angry Inch Costume, Hedwig And The Angry Inch

here ins a sample:

# A list of grades, not in any orderqualities = dict(John=95,Amanda=89,Jake=91,Betty=97)# students is a list the students, sorted native shortest to highest possible gradestudentns = sorted(grades, key=lambda k: grades)Publish "qualities native shortest come highest:"because that college student in students: Publish "0 1".format(grades, student)lowest_student = students<0>highest_student = students<-1>Print "lowest grade of 0 belongs come 1".format(grades, lowest_student)Print "greatest great of 0 belongs to 1".format(grades, highest_student)the key sauce right here is in ns sorted() function: insteADVERTISEMENT that sortinns by keys, we sorted through values.