I have checked all of the other questions with the same error yet found no helpful solution =/
I have a dictionary of lists:
d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]}
in which some of the values are empty. At the end of creating these lists, I want to remove these empty lists before returning my dictionary. Current I am attempting to do this as follows:
for i in d: if not d[i]: d.pop(i)
however, this is giving me the runtime error. I am aware that you cannot add/remove elements in a dictionary while iterating through it...what would be a way around this then?
The Python "RuntimeError: dictionary changed size during iteration" occurs when we change the size of a dictionary when iterating over it. To solve the error, use the copy() method to create a shallow copy of the dictionary that you can iterate over, e.g. my_dict. copy() .
It will not display the output because the computer ran out of memory before reaching 2^27. So there is no size limitation in the dictionary.
Iterate keys in dictionary ( dict ): keys() As mentioned above, you can iterate keys by using the dictionary object directly, but you can also use keys() . The result is the same, but keys() may clarify the intent to the reader of the code. The keys() method returns dict_keys .
How do you check if a key exists or not in a dictionary? You can check if a key exists or not in a dictionary using if-in statement/in operator, get(), keys(), handling 'KeyError' exception, and in versions older than Python 3, using has_key(). 2.
In Python 3.x and 2.x you can use use list
to force a copy of the keys to be made:
for i in list(d):
In Python 2.x calling keys
made a copy of the keys that you could iterate over while modifying the dict
:
for i in d.keys():
But note that in Python 3.x this second method doesn't help with your error because keys
returns an a view object instead of copynig the keys into a list.
You only need to use "copy":
On that's way you iterate over the original dictionary fields and on the fly can change the desired dict (d dict). It's work on each python version, so it's more clear.
(BTW - Generally to iterate over copy of your data structure, instead of using ".copy" for "dict" or slicing "[:]" for "list", you can use import copy
-> copy.copy
(for shallow copy which equivalent to "copy" that supported by "dict" or slicing "[:]" that supported by "list") or copy.deepcopy
on your data structure).
In [1]: d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]} In [2]: for i in d.copy(): ...: if not d[i]: ...: d.pop(i) ...: In [3]: d Out[3]: {'a': [1], 'b': [1, 2]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With