How to Remove a Key From Dictionary in Python

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

Output

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

Output

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

Output

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

Output

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

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.