Skip to Content

Remove Key From a Python Dictionary

Dictionaries in Python are used to store key -> value mappings. When working with dictionaries, it can be handy to know how to quickly delete a key from the dictionary. In this tutorial, we will look at how to remove a key from a Python dictionary with the help of some examples.

How to remove a key from a dictionary in Python?

You can use the Python dictionary pop() function to remove a key from a dictionary. Pass the key you want to remove as an argument to this function. You can also pass a default value (optional argument) that will be returned if the key is not present in the dictionary.

The following is the syntax –

# remove key from dictionary my_dict, return default if key is not present
my_dict.pop(key, default)

If the key is present in the dictionary, it removes the key and returns its value, else it returns the default. If the default is not specified and the key is not present, it raises a KeyError.

Let’s look at an example.

We have a dictionary containing the names -> department mappings of employees in an office. Suppose the employee “Jim” quits and you want to remove his record from the dictionary.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# remove key from dictionary
result = employees.pop("Jim")
print(result)
# display the dictionary
print(employees)

Output:

Sales
{'Dwight': 'Sales', 'Angela': 'Accounting'}

Here we delete the key “Jim” from the employees dictionary using the dictionary pop() function. You can see that the dictionary now does not have the key “Jim”.

What happens if you try to remove a key that is not present using the pop() function?

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# remove key from dictionary
result = employees.pop("Ryan")
print(result)
# display the dictionary
print(employees)

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [2], in <module>
      2 employees = {
      3     "Jim": "Sales",
      4     "Dwight": "Sales",
      5     "Angela": "Accounting"
      6 }
      7 # remove key from dictionary
----> 8 result = employees.pop("Ryan")
      9 print(result)
     10 # display the dictionary

KeyError: 'Ryan'

Here we try to remove the key “Ryan”, which is not present in the dictionary. Since we did not specify a default value in the pop() function to return in case the key is not present, we get a KeyError.

You can specify a default value to return if the key is not present in the dictionary when using the pop() function.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# remove key from dictionary
result = employees.pop("Ryan", None)
print(result)
# display the dictionary
print(employees)

Output:

None
{'Jim': 'Sales', 'Dwight': 'Sales', 'Angela': 'Accounting'}

Here we get None as the output because we specify our “default” value to return if the key is not present to be None.

Using the del keyword to remove key from dictionary

Alternatively, you can also use the del keyword to remove a key from a dictionary. Let’s use the same example as above.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# remove key from dictionary
del employees["Jim"]
# display the dictionary
print(employees)

Output:

{'Dwight': 'Sales', 'Angela': 'Accounting'}

You can see that the dictionary now does not contain the key “Jim”.

Note that if you use the del keyword with a key that is not present in the dictionary you’ll get a KeyError.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# remove key from dictionary
del employees["Ryan"]
# display the dictionary
print(employees)

Output:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [6], in <module>
      2 employees = {
      3     "Jim": "Sales",
      4     "Dwight": "Sales",
      5     "Angela": "Accounting"
      6 }
      7 # remove key from dictionary
----> 8 del employees["Ryan"]
      9 # display the dictionary
     10 print(employees)

KeyError: 'Ryan'

Here we get a KeyError since the key we are trying to delete is not present in the dictionary. To prevent the error, you can delete the key only if it’s present in the dictionary or use it in a tryexcept block.

# remove key from dictionary
if key in employees:
    del employees[key]

# # alternatively use a try except block
# try:
#     del employees[key]
# except KeyError:
#     print(f"Key {key} not present in the dictionary")
    
# display the dictionary
print(employees)

Output:

{'Jim': 'Sales', 'Dwight': 'Sales', 'Angela': 'Accounting'}

We did not get an error here.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.