Last Updated on June 22, 2021 by Roshan Parihar
In this tutorial, learn how to remove a key from Dictionary in Python. The short answer is to use the pop()
function of Python that takes an argument as the key to delete from the Dictionary variable.
You can also use the del
keyword to delete the specified key from Dictionary. Let’s find out with the examples given below here.
Method 1: Using pop() to Remove a Key From Dictionary in Python
To remove the specified key from Dictionary, you can use the pop()
. It takes two arguments to pass to the function to get the required result. The first argument is the key that you have to remove from the Dictionary variable. The second argument is useful to stay out of error messages when the specified is not exist.
If the key is a string, you have enclosed it within the single quotes (‘). While, if the key is a number, you can specify it without the quotes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#Declare a Dictionary variable in Python myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; #Print dictionary print("Old dictionary is: "+str(myDict)) #Remove key from Dictionary using pop() removeKey = myDict.pop('one', None) #Print new Dictionary print("New dictionary is: "+str(myDict)) #Print removed element print("Removed key's value is: "+str(removeKey)) |
Output
New dictionary is: {‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
Removed key’s value is: Sally
The above example contains 4 elements in the dictionary variable before the code execution. After the execution of the code, the Dictionary variable showing only the 3 elements.
Because the specified key is removed from the Dictionary variable using the pop()
. is a string, you have to pass it as an argument and within single quotes(‘). If the specified key is not present in the variable, it displays the second argument of the pop()
. Hence, the output gives the result without any error messages.
Method 2: Using del to Delete a Key From Dictionary in Python
The del
keyword can be used to delete the specified key from the Dictionary variable. To delete the required key, you have to use the del
keyword followed by the dictionary variable and the key within the square brackets ([]
).
If the key is a string, you have to enclose it within the double quotes (‘). For the numeric key, you have to specify it without quotes.
1 2 3 4 5 6 7 8 9 10 11 |
#Declare a Dictionary variable in Python myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; #Print dictionary print("Old dictionary is: "+str(myDict)) #Delete key from Dictionary using del del myDict['one'] #Print new Dictionary print("New dictionary is: "+str(myDict)) |
Output
New dictionary is: {‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
The output shows that the specified key is deleted from the Dictionary variable. It also gives the error message in the output if the specified key is not present in the Dictionary.
If the specified key is not present in the Dictionary, it gives an exception in the output. However, you can handle the exception error using the try and catch as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#Declare a Dictionary variable in Python myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; #Print dictionary print("Old dictionary is: "+str(myDict)) #Delete key from Dictionary using del try: del myDict['six'] except KeyError: print("The specified key is not found.") #Print new Dictionary print("New dictionary is: "+str(myDict)) |
Output
The specified key is not found.
New dictionary is: {‘one’: ‘Sally’, ‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
The above example shows that the specified key is not found. It gives no error and displays a string that is mentioned in the except
keyword.
Method 3: Using items()in Python
In addition to the above methods, you can also use the items()
of Python to eliminate the required key from the Dictionary variable. You have to use the function and specify the key as given in the example below.
1 2 3 4 5 6 7 8 9 10 11 |
#Declare a Dictionary variable in Python myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; #Print dictionary print("Old dictionary is: "+str(myDict)) #Remove key from Dictionary using items() newDict = {key:value for key, value in myDict.items() if key != "one"} #Print result print("New dictionary is: "+str(newDict)) |
Output
New dictionary is: {‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
The resulted Dictionary contains all the key elements except the key ‘one’ that is removed using the items()
.
I have covered almost all the available methods here to remove or delete the specified key from the Dictionary.
Do you have any other methods to remove a key from Dictionary using Python?. If yes, please comment below. I will add it in this post to help the readers.
You May Also Like to Read