VOOZH about

URL: https://www.geeksforgeeks.org/python/python-remove-item-from-dictionary-by-value/

⇱ Python Remove Item from Dictionary by Value - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Remove Item from Dictionary by Value

Last Updated : 23 Jul, 2025

We are given a dictionary and our task is to remove key-value pairs where the value matches a specified target. This can be done using various approaches, such as dictionary comprehension or iterating through the dictionary. For example: d = {"a": 10, "b": 20, "c": 10, "d": 30} and we have to remove the items with value "10" then the output will be {"b": 20, "d": 30}

Using Dictionary Comprehension

Dictionary comprehension is an efficient way to filter out key-value pairs by value. It creates a new dictionary containing only the items we want to keep.


Output
{'a': 1, 'c': 3}

Explanation: We iterate over all key-value pairs using d.items() and the condition v != 2 ensures that only items with values not equal to 2 are included in the new dictionary, then the original dictionary is updated with the filtered result.

Let's explore some more methods and see how we can remove item from dictionary by value.

Using del()

This method involves finding the key associated with the value and then using the del() keyword to remove the key-value pair.


Output
{'a': 1, 'c': 3}

Explanation:

  • next() function retrieves the first key where the value is 2.
  • If a key is found, the del statement removes the corresponding key-value pair.
  • This method avoids unnecessary iteration if the value is found early.

Using for Loop and pop()

This approach involves iterating through the dictionary to find the key with the specified value using for loop and then using pop() to remove it.


Output
{'a': 1, 'c': 3}

Explanation:

  • We use list(d.items()) to create a copy of the dictionary items for safe iteration and the if v == 2 condition identifies the key-value pair to remove.
  • pop() method removes the key-value pair from the dictionary.

Using filter() and dict()

The filter() function can also be used to create a filtered version of the dictionary by excluding items with the specified value.


Output
{'a': 1, 'c': 3}

Explanation:

  • filter() function applies a condition to each item in d.items() and only items where the value is not 2 are retained.
  • result is converted back to a dictionary using dict().
Comment
Article Tags: