|
| Home |
|---|
The Python dictionary pop() method removes a key-value pair from a dictionary using its key, and returns the value that was removed. It is the standard way to delete a specific dictionary entry in Python when you also need to use or store its value.
In this tutorial you will learn the syntax of dict.pop(),
how to use its optional default value to avoid errors, what happens
when the key does not exist, and how pop() compares to
other ways of removing dictionary items such as del and
popitem().
Dictionaries in Python are mutable, which means their contents can be
changed after creation, including removing entries entirely.
pop() is the most commonly used method for this, because
unlike del, it also hands back the value that was
removed — which is useful any time you need to process, log, or
reuse that value as part of removing it.
The syntax of the pop() method is shown below.
dictionary.pop(key, default_value)
pop() returns the value associated with the removed key.
If the key is missing and a default_value was provided,
it returns that default instead. If the key is missing and no
default was provided, Python raises a KeyError.
In this example we remove the key age from the dictionary.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.pop("age")
print(student)
{'name': 'Alice', 'course': 'Python'}
The key age is removed from the dictionary, along with
its value.
Unlike the del keyword, pop() gives you
access to the value it deleted, so you can store or use it right
away.
student = {
"name":"Alice",
"age":21
}
value = student.pop("age")
print(value)
21
The method removes the key age and returns its value,
21, which is then stored in the variable
value.
If the key does not exist, a default value can be provided as the second argument to prevent the program from crashing.
student = {
"name":"Alice"
}
print(student.pop("age","Key Not Found"))
Key Not Found
Since "age" is not a key in the dictionary,
pop() safely returns the default value
"Key Not Found" instead of raising an error, and the
dictionary itself remains unchanged.
If the key does not exist and no default value is given,
pop() raises a KeyError. This is one of
the most common runtime errors beginners run into with dictionaries.
student = {
"name":"Alice"
}
print(student.pop("age"))
KeyError: 'age'
To avoid this, always supply a default value with
pop() whenever the key might not exist — for
example, student.pop("age", None).
The pop() method can be called multiple times to remove
several keys from the same dictionary.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.pop("age")
student.pop("course")
print(student)
{'name': 'Alice'}
After removing two keys, only one key-value pair remains in the dictionary.
pop() is often used when cleaning or filtering
dictionary data — for example, removing every key that matches
a certain condition.
scores = {
"alice":45,
"bob":88,
"charlie":30
}
failing = [name for name, score in scores.items() if score < 50]
for name in failing:
scores.pop(name)
print(scores)
{'bob': 88}
Here the failing keys are first collected into a separate list, then
removed with pop(). Popping keys directly while looping
over scores.items() or scores.keys() would
raise a RuntimeError, since Python does not allow a
dictionary to change size while it is being iterated.
pop() can also be used to remove a key from a nested
dictionary by first accessing the inner dictionary.
students = {
"student1":{"name":"Alice", "age":21},
"student2":{"name":"Bob", "age":22}
}
students["student1"].pop("age")
print(students)
{'student1': {'name': 'Alice'}, 'student2': {'name': 'Bob', 'age': 22}}
Only the age key inside student1 is
removed; the rest of the nested structure stays unchanged.
Python offers several ways to remove dictionary data, and each behaves differently:
pop(key) — removes a specific key and returns its value; supports a default value to avoid errors.del dictionary[key] — removes a specific key but does not return the value, and raises a KeyError with no way to supply a default.popitem() — removes and returns the last inserted key-value pair as a tuple; you cannot choose which key to remove.clear() — removes every key-value pair, leaving an empty dictionary.
Use pop() whenever you need to remove one known key
safely and want to keep its value.
pop() runs in O(1) average time,
because Python dictionaries are implemented using a hash table,
which allows keys to be located and removed without scanning the
entire dictionary.
pop() on a key that does not exist without providing a default value, causing a KeyErrorpop() returns the removed value and can be assigned to a variablepop(), which removes a specific key, with popitem(), which removes the last inserted itempop() while directly looping over it, causing a RuntimeErrorpop() when the key is always expected to exist, instead of using the simpler del dictionary[key]
Yes. pop() removes the key-value pair directly from the
dictionary it is called on; it does not return a new dictionary.
Without a default value, it raises a KeyError, since
there are no keys to remove. With a default value, it simply returns
that default.
pop() removes a key you specify and returns its value.
popitem() does not take a key argument — it
removes and returns the most recently inserted key-value pair as a
tuple.
No, pop() only removes one key per call. To remove
multiple keys, call pop() once for each key, or loop
through a list of keys to remove.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net