How to delete items from a dictionary while iterating over it?

Can I delete items from a dictionary in Python while iterating over it?

I want to remove elements that don't meet a certain condition from the dictionary, instead of creating an entirely new dictionary. Is the following a good solution, or are there better ways?

for k, v in mydict.items():
    if k == val:
        del mydict[k]

← Назад к списку