|
| Home |
|---|
The Python dictionary popitem() method removes and returns the last inserted key-value pair from a dictionary, as a tuple. It is the standard way to remove dictionary items in last-in, first-out (LIFO) order, which makes it useful for undo operations, stack-like processing, and emptying a dictionary one item at a time.
In this tutorial you will learn the syntax of
dict.popitem(), how it behaves differently from
pop(), what happens when it is called on an empty
dictionary, and how to use it safely inside loops.
Since Python 3.7, dictionaries maintain insertion order
— keys keep the order in which they were added. The
popitem() method takes advantage of this by always
removing the most recently added key-value pair,
similar to how .pop() works on a list or a stack. Unlike
pop(), it does not take a key argument — you cannot
choose which item to remove.
The syntax of the popitem() method is shown below.
dictionary.popitem()
The popitem() method does not accept any parameters. It
always removes the last inserted key-value pair.
popitem() returns the removed key-value pair as a
tuple, in the form (key, value). If the
dictionary is empty, it raises a KeyError.
In this example we remove the last inserted element from a dictionary.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.popitem()
print(student)
{'name': 'Alice', 'age': 21}
The last inserted pair, "course":"Python", was removed
from the dictionary. The name and age keys,
inserted earlier, remain untouched.
The method returns the removed key-value pair as a tuple, which can be stored and used after removal.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
value = student.popitem()
print(value)
('course', 'Python')
The removed key-value pair is returned as a tuple, where the first element is the key and the second element is the value.
Because popitem() returns a two-item tuple, its key and
value can be unpacked directly into separate variables.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
key, value = student.popitem()
print(key)
print(value)
course
Python
Unpacking makes it easy to work with the removed key and value separately, without indexing into the tuple manually.
Calling popitem() repeatedly removes elements one by
one, always from the end of the dictionary, in reverse insertion
order.
data = {
"a":1,
"b":2,
"c":3
}
data.popitem()
data.popitem()
print(data)
{'a': 1}
Two key-value pairs were removed sequentially: first "c",
then "b", leaving only the first-inserted key,
"a".
A common use of popitem() is to process and remove every
item in a dictionary, one at a time, until it is empty.
data = {
"a":1,
"b":2,
"c":3
}
while data:
print(data.popitem())
('c', 3)
('b', 2)
('a', 1)
The while data: loop continues as long as the dictionary
is not empty. Each call to popitem() removes and prints
one key-value pair, and the loop ends automatically once the
dictionary has no items left.
Calling popitem() on an empty dictionary raises a
KeyError, since there is no item left to remove.
data = {}
data.popitem()
KeyError: 'popitem(): dictionary is empty'
Unlike pop(), popitem() does not accept a
default value, so this error cannot be avoided by passing a
fallback argument. Always check that the dictionary is not empty
before calling popitem(), or handle the error with a
try/except block.
To avoid crashing the program when a dictionary might be empty, wrap
popitem() in a try/except
block.
data = {}
try:
data.popitem()
except KeyError:
print("Dictionary is empty")
Dictionary is empty
This pattern is especially useful inside loops where you are unsure how many items remain in the dictionary.
These two methods are easy to confuse, but they behave differently:
pop(key) — removes a specific key you choose, and returns only its value. Supports a default value to avoid errors.popitem() — removes whichever key-value pair was inserted last, and returns both the key and value as a tuple. Does not support a default value.
Use pop() when you know exactly which key to remove.
Use popitem() when you want to remove items in
last-in-first-out order without specifying a key, such as when
processing a dictionary like a stack.
In older Python versions (before 3.7), dictionaries did not
guarantee insertion order, and popitem() was documented
as removing an arbitrary item. Since Python 3.7, dictionaries
preserve insertion order as a language guarantee, so
popitem() now reliably removes the
most recently inserted item every time. If your code
needs to support very old Python versions, avoid relying on this
LIFO behavior.
popitem() runs in O(1) time, since
Python dictionaries are implemented internally so that the last
inserted item can be located and removed without scanning the rest
of the dictionary.
popitem() to remove a specific key, when it always removes the last inserted item insteadpopitem(), which returns a key-value tuple, with pop(), which returns only a valuepopitem() on an empty dictionary without handling the resulting KeyErrorpopitem() accepts a default value like pop() does — it does notpopitem() to return a random item, which was only true in Python versions older than 3.7It removes the last inserted item, not the first. This has been guaranteed behavior since Python 3.7, when dictionaries became insertion-ordered.
No. popitem() does not accept any arguments. To remove a
specific key, use pop(key) instead.
It returns the removed key and value together as a tuple, in the
form (key, value).
Python raises a KeyError, since there is no item left
to remove. Use a try/except block or check
if data: before calling it.
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