How to remove a key from a dictionary python?

A dictionary is a collection of key-value pairs that are ordered, mutable, and doesn't allow redundant data. Dictionaries are written with curly brackets {}.

Consider a dictionary dict_1 = {1: 'A', 3: 'C', 2: 'B'}. After removing the key 2 from dict_1, the dictionary is modified to {1: 'A', 3: 'C'}. This article discusses various approaches to remove a key from dictionary in python.

Removing a key from a dictionary using del statement

The del statement removes a key from the dictionary. The syntax for the del statement to remove a key is as follows.

#syntax:
del dict[key]

Consider a dictionary color_dict = {1: 'red', 2: 'black', 3: 'green'}. del color_dict[2] removes {2: 'black'} from color_dict and modifies the color_dict to {1: 'red', 3: 'green'}

color_dict = {1: 'red', 2: 'black', 3: 'green'}
print("color_dict: ", color_dict)
del color_dict[2]
print("Removing key 2 from color_dict ", color_dict)

Output

color_dict:  {1: 'red', 2: 'black', 3: 'green'}
Removing key 2 from color_dict  {1: 'red', 3: 'green'}

Removing a key from a dictionary using the pop() function

The pop() function takes the key as a parameter and removes the specified key from the dictionary. The pop() function also returns the value of the removed key as output.

Consider a dictionary color_dict = {1: 'red', 2: 'black', 3: 'green'}. color_dict.pop(1) removes {1: 'red'} from color_dict and modifies the color_dict to {2: 'black', 3: 'green'} and returns 'red' to variable removed_val.

color_dict = {1: 'red', 2: 'black', 3: 'green'}
print("color_dict: ", color_dict)
removed_val = color_dict.pop(1)
print("removed_val: ", removed_val)
print("Removing key 2 from color_dict ", color_dict)

Output

color_dict:  {1: 'red', 2: 'black', 3: 'green'}
removed_val:  red
Removing key 2 from color_dict  {2: 'black', 3: 'green'}
The del statement and pop() function raises a KeyError exception if the key is not found in the dictionary.

For example, Consider a dictionary color_dict = {1: 'red', 2: 'black', 3: 'green'}. The statement del color_dict[5] or color_dict.pop(5) will raise a KeyError exception. To handle the errors we can use try and except block or if statement.

Using Try and except to handle KeyError exception

The try block tests the code, if any error is raised except block is executed.

color_dict = {1: 'red', 2: 'black', 3: 'green'}
print("color_dict: ", color_dict)
try:
    color_dict.pop(5)
    print("color_dict: ", color_dict)
except KeyError:
    print("key Not Found")

Output

color_dict:  {1: 'red', 2: 'black', 3: 'green'}
key Not Found
Using if statement to handle KeyError exception

The in operator checks if the key is present in the color_dict. If the key is not found, else statement is executed.

color_dict = {1: 'red', 2: 'black', 3: 'green'}
print("color_dict: ", color_dict)
if 5 in color_dict:
    color_dict.pop(5)
    print("color_dict: ", color_dict)
else:
    print("Key not Found")

Output

color_dict:  {1: 'red', 2: 'black', 3: 'green'}
Key not Found

Removing a key from a dictionary using the for loop

Consider a dictionary color_dict = {1: 'red', 2: 'black', 3: 'green'}. In the below code we try to remove key 1 from the dictionary.

The variable res_dict is initialized to an empty dictionary. A for loop iterates over the dictionary color_dict. In each iteration the program checks, if the key value is not equal to 1. If the condition is True, the key-value pair is added to res_dict. If the key = 1, the key and its corresponding value are not added to res_dict.

color_dict = {1: 'red', 2: 'black', 3: 'green'}
print("color_dict: ", color_dict)
res_dict = {}
for key, value in color_dict.items():
    if key != 1:
        res_dict[key] = value
print("res_dict: ", res_dict)

Output

color_dict:  {1: 'red', 2: 'black', 3: 'green'}
res_dict:  {2: 'black', 3: 'green'}
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions