[Tutor] deleting elements of a dictionary

Michael C mysecretrobotfactory at gmail.com
Thu May 18 12:55:12 EDT 2017


I am trying to remove incorrect entries of my dictionary.
I have multiple keys for the same value,

ex,
[111]:[5]
[222]:[5]
[333]:[5}

and I have found out that some key:value pairs are incorrect, and the best
thing to do
is to delete all entries who value is 5. So this is what I am doing:

import numpy
read_dictionary = numpy.load('loc_string_dictionary.npy').item()

for n in read_dictionary:
    print(read_dictionary[n])
    if read_dictionary[n] == '5':
        del read_dictionary[n]


However, I get this error:
RuntimeError: dictionary changed size during iteration

and I can see why.

What's the better thing to do?

thanks!


More information about the Tutor mailing list