In my code I loop through the keys in a dictionary and if a key matches a condition (existence in another list) the key-value pair is deleted:
for key in my_dict:
if key in my_list:
del my_dict[key]
Problem is, when I run the code I get an error: 'dictionary changed size during iteration'. I realize I can't do it with:
for i in range(len(my_dict)):...
since key indices in my dictionary will change with every deletion.
Is there a way to delete elements in a dictionary without raising an error?