- I have a list l.
- I have a dictionary d.
I want to iterate over l. For any list-item, I want to iterate over the d.keys.
If some condition is met, I would like to 'update' my dictionary.
I have naively tried to nest two for-loops and put in an if-statement -- One cannot change the length of the object one is iterating over.
d = {'this': '1', 'is': '2', 'a': '3', 'list': '4'}
l = ['A', 'B', 'C', 'D', 'E']
for word in l:
for key in d.keys():
if len(key) < 2:#some condition
d.pop(key)
else:
print(word, key)
This is the output I get:
A this
A is
Traceback (most recent call last):
File "untitled3.py", line 6, in <module>
for key in d.keys():
RuntimeError: dictionary changed size during iteration