I wonder if there is simple way to remove one or more dictionary element(s) from a python dictionary by value.
We have a dictionary called myDict
:
myDict = {1:"egg", "Answer":42, 8:14, "foo":42}
and want to remove all items which values are equal to 42
.
Implementation suggestion:
Get a list of all keys of a certain value in
myDict
(See for instance get key by value in dictionary.)Delete this dict element or elements (based on the found keys) from
myDict
(For more information, see Delete an element from a dictionary.)
So, what do you think now is the most elegant and most “pythonic” way to implement this problem in Python?