Python Tutoring Online
 Home

Python Dictionary pop() Method

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().

Need Help Understanding Python Dictionaries?

Many Python learners struggle when modifying dictionary data inside real programs.

A Python tutor can help you understand how dictionary methods like pop() work and how to use them in real projects.

Get Python Tutoring Help

What Does pop() Do?

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.

Syntax

The syntax of the pop() method is shown below.

Python
dictionary.pop(key, default_value)

Parameters

Return 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.

Example 1: Basic pop() Example

In this example we remove the key age from the dictionary.

Python
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.pop("age")
print(student)

Output


{'name': 'Alice', 'course': 'Python'}

The key age is removed from the dictionary, along with its value.

Example 2: pop() Returns the Removed Value

Unlike the del keyword, pop() gives you access to the value it deleted, so you can store or use it right away.

Python
student = {
"name":"Alice",
"age":21
}
value = student.pop("age")
print(value)

Output


21

The method removes the key age and returns its value, 21, which is then stored in the variable value.

Example 3: pop() with a Default Value

If the key does not exist, a default value can be provided as the second argument to prevent the program from crashing.

Python
student = {
"name":"Alice"
}
print(student.pop("age","Key Not Found"))

Output


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.

Example 4: pop() Without a Default Raises a KeyError

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.

Python
student = {
"name":"Alice"
}
print(student.pop("age"))

Output


KeyError: 'age'

To avoid this, always supply a default value with pop() whenever the key might not exist — for example, student.pop("age", None).

Example 5: Removing Multiple Elements

The pop() method can be called multiple times to remove several keys from the same dictionary.

Python
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.pop("age")
student.pop("course")
print(student)

Output


{'name': 'Alice'}

After removing two keys, only one key-value pair remains in the dictionary.

Example 6: Using pop() Inside a Loop

pop() is often used when cleaning or filtering dictionary data — for example, removing every key that matches a certain condition.

Python
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)

Output


{'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.

Example 7: pop() with a Nested Dictionary

pop() can also be used to remove a key from a nested dictionary by first accessing the inner dictionary.

Python
students = {
"student1":{"name":"Alice", "age":21},
"student2":{"name":"Bob", "age":22}
}
students["student1"].pop("age")
print(students)

Output


{'student1': {'name': 'Alice'}, 'student2': {'name': 'Bob', 'age': 22}}

Only the age key inside student1 is removed; the rest of the nested structure stays unchanged.

pop() vs del vs popitem() vs clear()

Python offers several ways to remove dictionary data, and each behaves differently:

Use pop() whenever you need to remove one known key safely and want to keep its value.

Time Complexity of pop()

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.

Common Beginner Mistakes

Frequently Asked Questions

Does pop() modify the original dictionary?

Yes. pop() removes the key-value pair directly from the dictionary it is called on; it does not return a new dictionary.

What happens if I call pop() on an empty 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.

What is the difference between pop() and popitem()?

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.

Can pop() remove multiple keys at once?

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.

Related Python Dictionary Tutorials




Stuck Learning Python?

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.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net