Python Program to remove a key from dictionary
In this tutorial, we will learn to write a program to remove a key from a dictionary in Python. Keys in a dictionary are unique and should be of an immutable data type such as strings, numbers, or tuples. For a given dictionary with values, the task is to delete a key-value pair from the dictionary.
Look at the example to understand the input-output format.
Input: {'x': 20, 'y': 10, 'z': 90 } d='x'
Output: { 'y': 10, 'z': 90 }
To solve this problem in Python, we can use the following approaches:
- Using del keyword
- Using pop()
- Using items() and dictionary comprehension
Let us look at all the approaches in detail.
Approach 1: By Using del Keyword
In this approach, we will use the del keyword to delete the key that is present in the dictionary. The only drawback of this method is that if the key is not present in the dictionary the del keyword will raise an exception which has to be handled separately.
Follow the algorithm to understand the approach better.
Algorithm
Step 1- Initialise a dictionary with keys and values
Step 2- Print the original dictionary
Step 3- Use del to delete key from dictionary
Step 4- Print the dictionary after deletion
Python Program 1
Look at the program to understand the implementation of the approach. We have deleted the key 'C' from the dictionary by giving the command to delete 'C' using the del keyword.
dic= { 'A' : "John", 'B' : "Sam", 'C' : "Max", 'D' : "Jake" }
print("Dictionary before removing key: ",dic)
del dic['C']
print("Dictionary after removing key: ",dic)
Dictionary before removing key: {'A': 'John', 'B': 'Sam', 'C': 'Max', 'D': 'Jake'}
Dictionary after removing key: {'A': 'John', 'B': 'Sam', 'D': 'Jake'}
Approach 2: using pop() Method
The pop() can be used to remove a key and its value from a dictionary. The pop() method will also return the value that is being deleted. This approach is better than using del because pop() will not give any exception if the key is not present in the dictionary.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Initialise a dictionary with keys and values
Step 2- Print the original dictionary
Step 3- Declare a variable that will store the value that is being deleted
Step 4- Use pop() and give the key which has to be deleted
Step 5- Print the value which has been deleted
Step 6- Print the dictionary after performing deletion
Python Program 2
Look at the program to understand the implementation of the approach. We have deleted the key A from the dictionary and also displayed the value which has been deleted along with the key.
# delete key using pop()
dic= { 'A' : "John", 'B' : "Sam", 'C' : "Max", 'D' : "Jake" }
print("Dictionary before removing key: ",dic)
# using pop() to remove 'A'
rem_val= dic.pop('A')
print("Value that is removed: ", rem_val)
print("Dictionary after removing key: ",dic)
Dictionary before removing key: {'A': 'John', 'B': 'Sam', 'C': 'Max', 'D': 'Jake'}
Value that is removed: John
Dictionary after removing key: {'B': 'Sam', 'C': 'Max', 'D': 'Jake'}
Approach 3: items() and dictionary comprehension
The items() method is a method of the dictionary class. It returns both keys and values in a dictionary. The drawback of this method is that it creates a copy of the dictionary without the key that has to be deleted.
Dictionary comprehension is used to make a copy of the original dictionary which will not have the key-value pair that has to be deleted.
Follow the algorithm to understand the approach better.
Algorithm
Step 1- Initialise a dictionary with keys and values
Step 2- Print the original dictionary
Step 3- Create a new dictionary
Step 4- Using dictionary comprehension store all the values except which has to be deleted
Step 5- Print the dictionary after deletion
Python Program 3
Look at the program to understand the implementation of the approach. We have created a new dictionary that will store all the key-value pairs except the pair that has to be deleted. We have deleted B from the dictionary.
dic= { 'A' : "John", 'B' : "Sam", 'C' : "Max" }
print("Dictionary before removing key: ",dic)
# using dictionary comprehension to copy without 'B'
new_dic= { key:val for key, val in dic.items() if key!= 'B'}
print("Dictionary after removing key: ",new_dic)
Dictionary before removing key: {'A': 'John', 'B': 'Sam', 'C': 'Max'}
Dictionary after removing key: {'A': 'John', 'C': 'Max'}
Conclusion
In this tutorial, we have seen three different approaches for deleting a key from a dictionary. When the key is deleted the value along with the key is also deleted from the dictionary. We have seen how to use methods like pop() and items() to remove a key. We have also discussed how to use the del keyword.