Python Dictionary popitem() Method
The popitem() method removes one item from a dictionary and returns it.
It is useful when you want to both:
- remove an item
- get that removed item at the same time
In modern Python, popitem() removes the last inserted key-value pair. It also changes the original dictionary, so the item is no longer there after the method runs.
Quick example
data = {"a": 1, "b": 2, "c": 3}
item = data.popitem()
print(item)
print(data)
Output:
('c', 3)
{'a': 1, 'b': 2}
popitem() removes and returns the last inserted key-value pair as a tuple.
What popitem() does
popitem():
- removes one item from a dictionary
- returns the removed item as a tuple:
(key, value) - removes the last inserted item in modern Python
- changes the original dictionary
This means the dictionary becomes smaller after you call it.
Basic syntax
dictionary.popitem()
Important points:
popitem()takes no arguments- it always returns a 2-item tuple
- use the returned tuple if you need both the key and the value
If you want to remove a specific key, use pop() instead.
What gets returned
The value returned by popitem() is a tuple with two parts:
- the key
- the value
Example:
person = {"name": "Alice", "age": 25}
result = person.popitem()
print(result)
Output:
('age', 25)
You can store the result in one variable:
result = person.popitem()
Or unpack it into two variables:
key, value = person.popitem()
Simple example
Here is a basic example showing what gets removed and what remains in the dictionary:
scores = {"Tom": 80, "Ana": 95, "Leo": 88}
removed_pair = scores.popitem()
print("Removed:", removed_pair)
print("After removal:", scores)
Output:
Removed: ('Leo', 88)
After removal: {'Tom': 80, 'Ana': 95}
The last inserted item was "Leo": 88, so that is the pair that gets removed.
Using tuple unpacking
Because popitem() returns a tuple, tuple unpacking is often the easiest way to use it.
book = {"title": "Python Basics", "pages": 200}
key, value = book.popitem()
print("Key:", key)
print("Value:", value)
print("Dictionary:", book)
Output:
Key: pages
Value: 200
Dictionary: {'title': 'Python Basics'}
This is useful when you want to process removed items one at a time.
Order behavior
In current Python versions, popitem() removes the last inserted key-value pair.
Example:
data = {"x": 1, "y": 2, "z": 3}
print(data.popitem())
Output:
('z', 3)
This matters because dictionaries keep insertion order in modern Python.
As a beginner, do not think of popitem() as removing a random item. In current Python, it removes the most recently added one.
If you want to inspect dictionary contents without removing anything, see items(), keys(), and values().
Difference between popitem() and pop()
popitem() and pop() both remove data from a dictionary, but they work differently.
popitem()
- removes the last inserted item automatically
- does not need a key argument
- returns
(key, value)
pop()
- removes the item for a specific key
- needs the key name
- returns only the value
Example:
data = {"a": 1, "b": 2, "c": 3}
item = data.popitem()
print("popitem():", item)
value = data.pop("a")
print("pop('a'):", value)
print(data)
Output:
popitem(): ('c', 3)
pop('a'): 1
{'b': 2}
Use pop() when you know the key.
Use popitem() when you want to remove one item without naming the key.
What happens if the dictionary is empty
Calling popitem() on an empty dictionary raises an error:
data = {}
data.popitem()
Error:
KeyError: 'popitem(): dictionary is empty'
Fix 1: Check that the dictionary is not empty
data = {}
if data:
item = data.popitem()
print(item)
else:
print("Dictionary is empty")
Fix 2: Use try-except
data = {}
try:
item = data.popitem()
print(item)
except KeyError:
print("Cannot use popitem() on an empty dictionary")
If you are troubleshooting this error, see how to fix KeyError: popitem(): dictionary is empty.
When to use popitem()
popitem() is useful when:
- you want to remove and get one item at the same time
- you are processing items until a dictionary becomes empty
- dictionary order matters in your program
Example of processing items until the dictionary is empty:
tasks = {
"first": "wash dishes",
"second": "do homework",
"third": "read book"
}
while tasks:
key, value = tasks.popitem()
print(f"{key}: {value}")
Possible output:
third: read book
second: do homework
first: wash dishes
Because popitem() removes the last inserted item, this works like removing items from the end.
Common mistakes
Here are some common problems beginners run into with popitem():
- Calling
popitem()on an empty dictionary- This raises
KeyError.
- This raises
- Expecting
popitem()to remove a specific key- It does not let you choose the key.
- Use
pop()if you need that.
- Forgetting that
popitem()changes the original dictionary- The removed item is gone after the call.
- Assuming the return value is only the value
popitem()returns a(key, value)tuple.
Helpful debugging checks:
print(my_dict)
print(len(my_dict))
item = my_dict.popitem()
print(item)
Or with unpacking:
key, value = my_dict.popitem()
print(key)
print(value)
FAQ
What does popitem() return in Python?
It returns the removed key-value pair as a tuple, such as ('name', 'Alice').
Does popitem() remove the last item?
Yes. In modern Python, it removes the last inserted key-value pair.
Can I choose which key popitem() removes?
No. Use pop() if you want to remove a specific key.
Does popitem() change the original dictionary?
Yes. The item is removed from the dictionary.
What error does popitem() raise on an empty dictionary?
It raises KeyError with the message popitem(): dictionary is empty.