I have a dictionary, lets call it myDict, in Python that contains a set of similar dictionaries which all have the entry “turned_on : True” or “turned_on : False“. I want to remove all the entries in myDict that are off, e.g. where “turned_on : False“. In Ruby I would do something like this:
myDict.delete_if { |id,dict| not dict[:turned_on] }
How should I do this in Python?
Straight-forward way:
Testing:
The other answers on this page so far create another dict. They don’t delete the keys in your actual dict.