Home »
Python »
Python programs
How to remove a key from dictionary in Python?
Here, we have a dictionary and a key which is to be removed. We will see multiple ways to remove a key from a dictionary in Python.
Submitted by Shivang Yadav, on May 05, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Dictionary is a collection in python which is used to store data as Key:value pair.
Example:
dict = { "python" : 1, "C++" : 3, "javaScript" : 2 }
Remove a key from a dictionary in python
We have a dictionary data structure and we will take input from the user on the keys to be removed from it. We need to write a program to delete this key from the dictionary and print the updated dictionary.
We need to find the key and remove it. This can be done in python using multiple methods provided on collections. Let’s see them at work.
Input:
dict = { "python" : 1, "C++" : 3, "javaScript" : 2 } python
Output:
updated dictionary : { "C++" : 3, "javaScript" : 2 }
Method 1: Using pop() method on dictionary
To remove any key from python we can use the pop() method.
The pop() method on a dictionary is used to delete a key:value pair. The method returns the removed pair and if it does not exist it prints the string which we can give as an extra argument (otherwise throws an exception).
Syntax:
dictionary_name.pop("key")
Program to remove a key from dictionary using pop() method in Python
# Program to remove a key from dictionary
# using pop() method in python
# printing the dictionary and getting value to be deleted
langRating = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langRating)
delKey = input("Enter the key to be deleted ")
# Removing the key from dictionary
delVal = langRating.pop(delKey, "Wrong key entered")
# Printing the deleted key and the dictionary
print("The dictionary after deletion is : ", langRating)
print("The deleted value is :", delVal)
Output:
The dictionary is : {'java': 20, 'r': 21, 'c++': 22, 'python': 24, 'javaScript': 25}
Enter the key to be deleted java
The dictionary after deletion is : {'r': 21, 'c++': 22, 'python': 24, 'javaScript': 25}
The deleted value is : 20
Method 2: Using del
Another method that can be used to delete a key from a dictionary is using the del. We can use the del keyword followed by the dict[key] notation to remove the specific key from the dictionary.
del does not accept any extra value to be returned in case of false value. It will instead return an error.
Program to remove key from dictionary using del
# Program to remove a key from dictionary
# using del in Python
# printing the dictionary and getting value to be deleted
langRating = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langRating)
delKey = input("Enter the key to be deleted ")
# Removing the key from dictionary
del langRating[delKey]
# Printing the dictionary
print("The dictionary after deletion is : ", langRating)
Output:
The dictionary is : {'python': 24, 'java': 20, 'javaScript': 25, 'c++': 22, 'r': 21}
Enter the key to be deleted r
The dictionary after deletion is : {'python': 24, 'java': 20, 'javaScript': 25, 'c++': 22}
Method 3: By creating a new dictionary using comprehension
We will filter off the key:val while creating a new dictionary using comprehension in Python.
Program to remove a key from dictionary using comprehension
# Program to remove a key from dictionary
# using del in python
# printing the dictionary and getting value to be deleted
langRating = {"python" : 24, "c++" : 22, "javaScript" : 25, "java" : 20, "r" : 21 }
print("The dictionary is :", langRating)
delKey = input("Enter the key to be deleted ")
# Removing the key from dictionary
updatedDict = { key:val for key, val in langRating.items() if key != delKey}
# Printing the dictionary
print("The dictionary after deletion is : ", updatedDict)
Output:
The dictionary is : {'c++': 22, 'javaScript': 25, 'python': 24, 'r': 21, 'java': 20}
Enter the key to be deleted r
The dictionary after deletion is : {'c++': 22, 'javaScript': 25, 'python': 24, 'java': 20}
Explanation:
In the above code, we have deleted a key from the dictionary using comprehension. Comprehension is a built-in feature that allows the programmer to perform the task in a lesser number of lines of code.
With comprehension, we have filtered off the key which we want to delete from the dictionary. For this, we loop on easy items of the dictionary and add them to a new dictionary if the key is not equal to the key to be deleted.
Python Dictionary Programs »