Open In App

Python Remove Item from Dictionary by Value

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python, with its elegant syntax and powerful data structures, stands out as one of the most favored programming languages. Among its arsenal of data structures, the dictionary is a versatile and indispensable tool for developers. Dictionaries facilitate the storage of key-value pairs, allowing for efficient data retrieval and manipulation. In this article, we will see how we can remove item from dictionary by value in Python.

Remove Item from Dictionary by Value in Python

Below are some of the ways by which we can remove item from a dictionary by value in Python:

  1. Using a Dictionary Comprehension
  2. Using dict.items() and filter()
  3. Using a Loop

Remove Item from Dictionary Using a Dictionary Comprehension

In this example, a function named remove_items_by_value is defined to remove key-value pairs from a dictionary where the values match a specified value (value_to_remove). The function creates a new dictionary (new_dict) by filtering out the key-value pairs with the specified value, and an example usage demonstrates its application by removing key-value pairs with the value of 2 from the original dictionary.

Python3




def remove_items_by_value(dictionary, value_to_remove):
    new_dict = {key: value for key, value in dictionary.items()
                if value != value_to_remove}
    return new_dict
 
 
# Example usage
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
value_to_remove = 2
modified_dict = remove_items_by_value(original_dict, value_to_remove)
 
print(modified_dict)


Output

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

Time Complexity: O(n), where n is the number of items in the original dictionary.
Space Complexity: O(n), as it creates a new dictionary.

Remove Item from Dictionary Using dict.items() and filter()

In this example, the function remove_items_by_value takes a dictionary and a specified value (value_to_remove). It uses the filter() function with a lambda() expression to create a new dictionary (new_dict) by excluding key-value pairs where the value matches the specified value.

Python3




def remove_items_by_value(dictionary, value_to_remove):
    new_dict = dict(
        filter(lambda item: item[1] != value_to_remove, dictionary.items()))
    return new_dict
 
 
# Example usage
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
value_to_remove = 3
modified_dict = remove_items_by_value(original_dict, value_to_remove)
 
print(modified_dict)


Output

{'a': 1, 'b': 2, 'd': 2}

Time Complexity: O(n), where n is the number of items in the original dictionary.
Space Complexity: O(n), as it creates a new dictionary.

Remove Item from Dictionary Using a Loop

In this example, the function remove_items_by_value takes a dictionary and a specified value (value_to_remove). It identifies the keys corresponding to the specified value and removes them from the original dictionary.

Python3




def remove_items_by_value(dictionary, value_to_remove):
    keys_to_remove = [key for key,
                      value in dictionary.items() if value == value_to_remove]
    for key in keys_to_remove:
        del dictionary[key]
    return dictionary
 
 
# Example usage
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
value_to_remove = 2
remove_items_by_value(original_dict, value_to_remove)
 
print(original_dict)


Output

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

Time Complexity: O(n), where n is the number of items in the original dictionary.
Space Complexity: O(k), where k is the number of keys to remove. It creates a separate list of keys

Conclusion

Removing items from a dictionary by value is a common operation in Python. Depending on your preferences and coding style, you can choose from various methods to achieve this task. Whether using dictionary comprehensions, filtering, or loops, these approaches allow you to manipulate dictionaries efficiently in your Python programs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads