Python Dictionary – Remove key based on value

Python Dictionary – Remove key based on value

You can remove a key from a dictionary based on the specified value in Python using Dictionary comprehension or a For loop.

In this tutorial, you will learn how to remove a key from a dictionary based on a given value, with examples.

For example, consider the following dictionary.

{
    'foo':12,
    'bar':14
}

'foo' and 'bar' are keys. 12 and 14 are values. If we would like to remove item with value 14 from the dictionary, the resulting dictionary should be as shown below.

{
    'foo':12,
}

The key 'bar' corresponding to the given value 14 should be removed from the dictionary.

1. Removing key based on value using Dictionary Comprehension in Python

Consider that you are given a dictionary my_dict with some initial key-value pairs. You have to remove the key that has the value of 14 from the dictionary using Dictionary Comprehension.

Steps

  1. Given a dictionary in my_dict, and value to be removed in value_to_remove.
  2. Use dictionary comprehension, and add the key value of original dictionary my_dict to the resulting dictionary, if the value is not equal to given 14.
  3. Store the returned dictionary in a variable, say result_dict. You may print the resulting dictionary to output using print() built-in function.

Program

The program to removing key based on specified value from a dictionary is given below.

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

value_to_remove = 14

result_dict = {key: value for key, value in my_dict.items() if value != value_to_remove}
print(result_dict)
Run Code Copy

Output

{'foo': 12}

If there are multiple keys with the same given value, then all those keys would be removed in the resulting dictionary. For example, in the following program, the dictionary my_dict has two keys with the same value 14. These two keys shall be removed in the resulting dictionary.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':14,
}

value_to_remove = 14

result_dict = {key: value for key, value in my_dict.items() if value != value_to_remove}
print(result_dict)
Run Code Copy

Output

{'foo': 12}

2. Removing key based on value from dictionary using For loop in Python

In the following program, you are given a dictionary my_dict with some initial key-value pairs. You have to remove the key that has the value of 14 from the dictionary using For loop.

Steps

  1. Given a dictionary in my_dict, and value to be removed in value_to_remove.
  2. Take an empty dictionary in result_dict.
  3. Use For loop to iterate over the items in the original dictionary my_dict.
    1. Inside the For loop, for each item in the original dictionary, add this item to the result_dict if the value in the item is not equal to the specified value value_to_remove.
  4. After the completion of For loop, result_dict would contain the items from original dictionary whose value is not equal to the specified value.

Program

Python Program

my_dict = {
    'foo':12,
    'bar':14
}

value_to_remove = 14

result_dict = {}
for key, value in my_dict.items():
    if value != value_to_remove:
        result_dict[key] = value

print(result_dict)
Run Code Copy

Output

{'foo': 12}

Summary

In this tutorial, we learned how to remove keys based on value from a dictionary using Dictionary Comprehension, or a For loop, with the help of well detailed Python programs.

Code copied to clipboard successfully 👍