Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to remove a dictionary item by value in python? [duplicate]

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:

  1. Get a list of all keys of a certain value in myDict
    (See for instance get key by value in dictionary.)

  2. 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?

like image 641
elegent Avatar asked Mar 23 '15 19:03

elegent


People also ask

How do I remove duplicates from a dictionary list?

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.

Does dictionary remove duplicates Python?

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 dictionary?

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,

Is it safe to delete a key from a Python dictionary?

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.

How to delete a dictionary in Python with clear () method?

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

How to delete a key from a dictionary 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.


2 Answers

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'} 
like image 61
A.J. Uppal Avatar answered Sep 20 '22 09:09

A.J. Uppal


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'} ` 
like image 25
cruane Avatar answered Sep 23 '22 09:09

cruane