How to Remove a Key From Dictionary Using Python

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.

Output

Old dictionary is: {‘one’: ‘Sally’, ‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
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.

Output

Old dictionary is: {‘one’: ‘Sally’, ‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
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.

Output

Old dictionary is: {‘one’: ‘Sally’, ‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
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.

Output

Old dictionary is: {‘one’: ‘Sally’, ‘two’: 13, ‘three’: ‘Dingra’, ‘four’: ‘Lilop’}
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

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.