Python Remove Item from Dictionary by Value Last Updated : 20 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ComprehensionDictionary 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. Python d = {'a': 1, 'b': 2, 'c': 3} # Removing the item with value 2 d = {k: v for k, v in d.items() if v != 2} print(d) 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.Table of ContentUsing del() with Reverse LookupUsing for Loop and pop()Using filter() and dict()Using del() This method involves finding the key associated with the value and then using the del() keyword to remove the key-value pair. Python d = {'a': 1, 'b': 2, 'c': 3} # Finding the key with value 2 key_rem = next((k for k, v in d.items() if v == 2), None) # Removing the key-value pair if the key exists if key_rem: del d[key_rem] print(d) 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. Python d = {'a': 1, 'b': 2, 'c': 3} # Looping to find and remove the item for k, v in list(d.items()): if v == 2: d.pop(k) break print(d) 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. Python d = {'a': 1, 'b': 2, 'c': 3} # Filtering out items with value 2 d = dict(filter(lambda item: item[1] != 2, d.items())) print(d) 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 More infoAdvertise with us Next Article Set from Dictionary Values - Python T tejas342 Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Python - Remove Item from Dictionary There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict 3 min read Python Remove Item from Dictionary by Key Dictionaries in Python store data as key-value pairs. Often, we need to remove a specific key-value pair to modify or clean the dictionary. For instance, consider the dictionary d = {'a': 1, 'b': 2, 'c': 3}; we might want to remove the key 'b'. Let's explore different methods to achieve this.Using d 3 min read Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop 2 min read Set from Dictionary Values - Python The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set.For example, given a dictionary like d = {'A': 4, ' 3 min read Dictionary items in value range in Python In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop.Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items 2 min read Sort Python Dictionary by Value Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, a 3 min read Like