Last Updated on May 9, 2024 by Roshan Parihar
To remove a key from Dictionary in Python, use the pop()
function 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.
How to Remove a Key From Dictionary Using pop() Function in Python?
If you want to remove the specified key from the Dictionary, you can use the pop()
function in Python. It takes two arguments in which the first argument is the key that you have to remove. The second argument is useful to stay out of error messages when the specified does not exist.
Examples 1
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
The new dictionary is: {‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
The removed key’s value is: Sally
The above example contains 4 elements in the dictionary variable before the code execution. After the removal of the specified key, the Dictionary variable shows only the 3 elements.
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 ([]
).
Examples 2
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
The 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.
Examples 3
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.
The 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.
Using items() Function 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.
Examples 2
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
The new dictionary is: {‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
The resulting Dictionary contains all the key elements except the key ‘one’ that is removed using the items()
.
You May Also Like to Read