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?
The strategy is to convert the list of dictionaries to a list of tuples where the tuples contain the items of the dictionary. Since the tuples can be hashed, you can remove duplicates using set (using a set comprehension here, older python alternative would be set(tuple(d.
Dictionaries in Python cannot include duplicate keys. If we convert our list to a dictionary, it will remove any duplicate values. That's where the fromkeys() method comes in.
What is the best way to remove an item from a Python dictionary? You can use the del function to delete a specific key or loop through all keys and delete them. For example,
Because the comprehension uses an if statement, we can say that this is a safe method of deleting a key from a Python dictionary. Python makes it easy to remove multiple keys from a dictionary.
Python has in-built clear () method to delete a dictionary in Python. The clear () method deletes all the key-value pairs present in the dict and returns an empty dict. The following techniques can be used to delete a key-value pair from a dictionary: 1. Using pop () method
The pop () method basically accepts a key to be deleted from the dictionary. It deletes the key as well as the value associated with the key from the dict and returns the updated dict. In the above snippet of code, we have passed the key – “A” as an argument to the pop () method. Thus, it deletes the key value pair associated with “A”. 2.
You can use a simple dict
comprehension:
myDict = {key:val for key, val in myDict.items() if val != 42}
As such:
>>> {key:val for key, val in myDict.items() if val != 42} {8: 14, 1: 'egg'}
You must create a copy to iterate over as changing the size of the dictionary inside of a loop causes a RunTimeError. Iterate over key, value pairs in your dictionary copy using items() and compare each value to the value you are looking for. If they match, delete the key from the dictionary.
for key, value in dict(myDict).items(): if value == 42: del mydict[key]
Adding answer for question in the comments below as it was too big for a comment. Here is a quick console session showing that mydict.copy()
and dict(myDict)
accomplish the same thing.
>>>import copy >>>dict1 = {1:"egg", "Answer":42, 8:14, "foo":42} >>>dict2 = dict(dict1) >>>dict3 = dict1.copy() >>>dict4 = dict1 >>>dict1[1] = "egg sandwich" >>>dict1 {'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 14} >>>dict2 {'Answer': 42, 1: 'egg', 'foo': 42, 8: 14} >>>dict3 {'Answer': 42, 1: 'egg', 'foo': 42, 8: 14} >>>dict4 {'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 14} >>>dict2['foo'] = "I pity the" dict1 >>>{'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 14} >>>dict2 {'Answer': 42, 1: 'egg', 'foo': 'I pity the', 8: 14} >>>dict3 {'Answer': 42, 1: 'egg', 'foo': 42, 8: 14} >>>dict4 {'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 14} >>>dict4[8] = "new" >>>dict1 {'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 'new'} >>>dict2 {'Answer': 42, 1: 'egg', 'foo': 'I pity the', 8: 14} >>>dict3 {'Answer': 42, 1: 'egg', 'foo': 42, 8: 14} >>>dict4 {'Answer': 42, 1: 'egg sandwich', 'foo': 42, 8: 'new'} `
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With