Python Tutoring Online
 Home

Python Dictionary popitem() Method

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.

Need Help Learning Python?

Dictionary operations like removing and updating elements can sometimes confuse beginners.

Working with a Python tutor can help you understand dictionary methods like popitem() and practice them with real coding exercises.

Get Python Tutoring Help

What Does popitem() Do?

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.

Syntax

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

Python
dictionary.popitem()

Parameters

The popitem() method does not accept any parameters. It always removes the last inserted key-value pair.

Return Value

popitem() returns the removed key-value pair as a tuple, in the form (key, value). If the dictionary is empty, it raises a KeyError.

Example 1: Basic popitem() Example

In this example we remove the last inserted element from a dictionary.

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

Output


{'name': 'Alice', 'age': 21}

The last inserted pair, "course":"Python", was removed from the dictionary. The name and age keys, inserted earlier, remain untouched.

Example 2: popitem() Returns the Removed Pair

The method returns the removed key-value pair as a tuple, which can be stored and used after removal.

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

Output


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

Example 3: Unpacking the Returned Tuple

Because popitem() returns a two-item tuple, its key and value can be unpacked directly into separate variables.

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

Output


course
Python

Unpacking makes it easy to work with the removed key and value separately, without indexing into the tuple manually.

Example 4: Using popitem() Multiple Times

Calling popitem() repeatedly removes elements one by one, always from the end of the dictionary, in reverse insertion order.

Python
data = {
"a":1,
"b":2,
"c":3
}
data.popitem()
data.popitem()
print(data)

Output


{'a': 1}

Two key-value pairs were removed sequentially: first "c", then "b", leaving only the first-inserted key, "a".

Example 5: Emptying a Dictionary with popitem()

A common use of popitem() is to process and remove every item in a dictionary, one at a time, until it is empty.

Python
data = {
"a":1,
"b":2,
"c":3
}
while data:
    print(data.popitem())

Output


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

Example 6: popitem() on an Empty Dictionary Raises a KeyError

Calling popitem() on an empty dictionary raises a KeyError, since there is no item left to remove.

Python
data = {}
data.popitem()

Output


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.

Example 7: Handling popitem() Safely with try/except

To avoid crashing the program when a dictionary might be empty, wrap popitem() in a try/except block.

Python
data = {}
try:
    data.popitem()
except KeyError:
    print("Dictionary is empty")

Output


Dictionary is empty

This pattern is especially useful inside loops where you are unsure how many items remain in the dictionary.

popitem() vs pop()

These two methods are easy to confuse, but they behave differently:

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.

Why popitem() Removes the Last Item, Not a Random One

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.

Time Complexity of popitem()

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.

Common Beginner Mistakes

Frequently Asked Questions

Does popitem() remove the first or last item in a dictionary?

It removes the last inserted item, not the first. This has been guaranteed behavior since Python 3.7, when dictionaries became insertion-ordered.

Can I choose which key popitem() removes?

No. popitem() does not accept any arguments. To remove a specific key, use pop(key) instead.

What does popitem() return?

It returns the removed key and value together as a tuple, in the form (key, value).

What happens if I call popitem() on an empty dictionary?

Python raises a KeyError, since there is no item left to remove. Use a try/except block or check if data: before calling it.

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