Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve dictionary changed size during iteration error?

I want 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?

like image 452
YXH Avatar asked Nov 22 '12 20:11

YXH


People also ask

Can you iterate over dictionary keys?

To iterate through the dictionary's keys, utilise the keys() method that is supplied by the dictionary. An iterable of the keys available in the dictionary is returned. Then, as seen below, you can cycle through the keys using a for loop.

How do I iterate through a dictionary item?

In Python, to iterate the dictionary ( dict ) with a for loop, use keys() , values() , items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() . Use the following dictionary as an example. You can iterate keys by using the dictionary object directly in a for loop.

Can a for loop iterate over a dictionary?

You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.


2 Answers

In Python3, Try

for key in list(dict.keys()):
    if condition:
        matched
        del dict[key]

1 more thing should be careful when looping a dict to update its key:

Code1:

keyPrefix = ‘keyA’
for key, value in Dict.items():
    newkey = ‘/’.join([keyPrefix, key])
    Dict[newkey] = Dict.pop(key)

Code2:

keyPrefix = ‘keyA’
for key, value in Dict.keys():
    newkey = ‘/’.join([keyPrefix, key])
    Dict[newkey] = Dict.pop(key)

Result of code1/code2 is:

{‘keyA/keyA/keyB’ : ”, ‘keyA/keyA/keyA’: ”}

My way to resolve this unexpected result:

    Dict = {‘/’.join([keyPrefix, key]): value for key, value in Dict.items()}

Link: https://blog.gainskills.top/2016/07/21/loop-a-dict-to-update-key/

like image 83
Xb74Dkjb Avatar answered Sep 27 '22 00:09

Xb74Dkjb


Alternative solutions

If you're looking for the smallest value in the dictionary you can do this:

min(dictionary.values())

If you cannot use min, you can use sorted:

sorted(dictionary.values())[0]

Why do I get this error?

On a side note, the reason you're experiencing an Runtime Error is that in the inner loop you modify the iterator your outer loop is based upon. When you pop an entry that is yet to be reached by the outer loop and the outer iterator reaches it, it tries to access a removed element, thus causing the error.
If you try to execute your code on Python 2.7 (instead of 3.x) you'll get, in fact, a Key Error.

What can I do to avoid the error?

If you want to modify an iterable inside a loop based on its iterator you should use a deep copy of it.

like image 24
Nadir Sampaoli Avatar answered Sep 23 '22 00:09

Nadir Sampaoli