How to Remove a Key from a Dictionary in Python
If you want to remove a key from a Python dictionary, the main tools are del and pop().
This page shows you:
- How to remove one key from a dictionary
- When to use
deland when to usepop() - How to avoid
KeyErrorif the key may not exist - What happens to the removed value
If you are new to dictionaries, see Python dictionaries explained.
Quick answer #
person = {"name": "Ana", "age": 25, "city": "Lima"}
# Remove a key and get its value
removed_value = person.pop("age")
print(person)
print(removed_value)
Output:
{'name': 'Ana', 'city': 'Lima'}
25
Use pop() when you want to remove a key and also get the removed value.
What this page helps you do #
- Remove one key from a dictionary
- Choose between
del,pop(), andpop(key, default) - Avoid
KeyErrorwhen the key may not exist - Understand what happens to the removed value
Use del when you only want to remove the key #
The del statement removes a key-value pair from a dictionary.
Use it when:
- You only want to delete the key
- You do not need the old value
Example:
person = {"name": "Ana", "age": 25, "city": "Lima"}
del person["age"]
print(person)
Output:
{'name': 'Ana', 'city': 'Lima'}
Important #
If the key does not exist, Python raises a KeyError.
person = {"name": "Ana", "city": "Lima"}
del person["age"] # KeyError
If you often see this error, read how to fix KeyError when accessing dictionary values.
Use pop() when you want the removed value #
The pop(key) method removes the key and returns its value.
This is useful when you want to:
- Save the value in a variable
- Print the removed value
- Use the value later in your code
Example:
person = {"name": "Ana", "age": 25, "city": "Lima"}
removed_age = person.pop("age")
print(person)
print(removed_age)
Output:
{'name': 'Ana', 'city': 'Lima'}
25
This is different from del:
delremoves the key onlypop()removes the key and gives you the old value
To learn more, see the Python dictionary pop() method.
Important #
If the key does not exist, pop(key) also raises KeyError.
person = {"name": "Ana", "city": "Lima"}
person.pop("age") # KeyError
Use pop(key, default) to avoid errors #
If you are not sure whether a key exists, use pop(key, default).
The second argument is a fallback value. Python returns it if the key is missing, instead of raising an error.
Example:
person = {"name": "Ana", "city": "Lima"}
removed_age = person.pop("age", "not found")
print(person)
print(removed_age)
Output:
{'name': 'Ana', 'city': 'Lima'}
not found
This is a safe option when:
- The key may or may not exist
- You want to avoid
KeyError - You want simple code
You can also use None as the default:
person = {"name": "Ana", "city": "Lima"}
removed_age = person.pop("age", None)
print(removed_age)
Check before removing a key #
Another beginner-friendly option is to check first.
Use if key in my_dict before del or pop() when you want clear logic or want to run different code if the key is missing.
Example:
person = {"name": "Ana", "city": "Lima"}
if "age" in person:
del person["age"]
else:
print("The key was not found.")
print(person)
Output:
The key was not found.
{'name': 'Ana', 'city': 'Lima'}
This approach is useful when you want to:
- Avoid errors
- Show a message
- Run other code if the key does not exist
If you need help with this check, see how to check if a key exists in a dictionary in Python.
Pick the right method #
Use the method that matches your situation:
- Use
delfor simple removal - Use
pop()to remove a key and return its value - Use
pop(key, default)when the key may be missing
A quick comparison:
person = {"name": "Ana", "age": 25, "city": "Lima"}
# 1. Simple removal
del person["city"]
# 2. Remove and return value
age = person.pop("age")
# 3. Safe removal if key may be missing
country = person.pop("country", "not found")
print(person)
print(age)
print(country)
Output:
{'name': 'Ana'}
25
not found
Common mistakes #
Here are some common problems beginners run into.
Using del with a key that is not in the dictionary #
This causes a KeyError.
data = {"name": "Ana"}
del data["age"] # KeyError
Using pop(key) without checking whether the key exists #
This also causes a KeyError.
data = {"name": "Ana"}
data.pop("age") # KeyError
Confusing removing a key with setting its value to None #
These are not the same.
data = {"name": "Ana", "age": 25}
data["age"] = None
print(data)
print("age" in data)
Output:
{'name': 'Ana', 'age': None}
True
The key still exists. Only the value changed.
If you want to read values safely, see how to access values in a dictionary in Python.
Trying to remove multiple keys with code meant for one key #
Methods like del my_dict["key"] and my_dict.pop("key") remove one key at a time.
If you need to remove everything, use clear():
data = {"name": "Ana", "age": 25, "city": "Lima"}
data.clear()
print(data)
Output:
{}
See the Python dictionary clear() method.
FAQ #
How do I remove a key without getting an error? #
Use pop(key, default) or check with if key in my_dict first.
What is the difference between del and pop()? #
del removes the key only. pop() removes the key and returns its value.
Does setting a key to None remove it? #
No. The key still exists. Only its value changes to None.
How do I remove all keys from a dictionary? #
Use clear() to remove every key-value pair.