Post

Python Dictionary popitem()

In the python dictionary, the popitem() method returns and removes the last pair of the given dictionary.

Python Dictionary popitem()

The popitem() method in Python dictionaries is a built-in function that removes and returns the last inserted key-value pair as a tuple. It is one of the most useful methods when you want to process and remove dictionary items one by one, especially when the order of removal matters. Understanding how popitem() works can help you write cleaner, more efficient Python code when working with dictionaries.

In Python 3.7 and above, dictionaries maintain insertion order. This means popitem() always removes the last inserted item — following the LIFO (Last In, First Out) principle. In older Python versions (before 3.7), the behavior was unpredictable because dictionaries were unordered.

Syntax of popitem()

The syntax of popitem() is:

1
dictionary.popitem()

popitem() Parameters

ParameterDescription
NoneThe popitem() method does not accept any parameters. Passing any argument will raise a TypeError.

The method takes no arguments. If you try to pass any parameter, Python will immediately raise a TypeError, so always call it as dictionary.popitem() without parentheses containing arguments.

Return Value

popitem() returns a tuple in the form (key, value) representing the last inserted pair. Once removed, the key-value pair is permanently deleted from the dictionary (unless you store the returned tuple).


Examples of popitem() in Python Dictionaries

Example 1: How to use the popitem() method in Python dictionaries?

1
2
3
4
5
6
7
8
9
10
cars = {"BMW": 1, "TOYOTA": 2, "TATA": 3}

print("Removed item is:", cars.popitem())
print("New Dictionary is:", cars)

print("Removed item is:", cars.popitem())
print("New Dictionary is:", cars)

print("Removed item is:", cars.popitem())
print("New Dictionary is:", cars)

Output:

1
2
3
4
5
6
Removed item is: ('TATA', 3)
New Dictionary is: {'BMW': 1, 'TOYOTA': 2}
Removed item is: ('TOYOTA', 2)
New Dictionary is: {'BMW': 1}
Removed item is: ('BMW', 1)
New Dictionary is: {}

In this example, every call to popitem() removes and returns the last item in the dictionary. Since Python 3.7 preserves insertion order, “TATA” (inserted last) is removed first, then “TOYOTA”, and finally “BMW”.


Example 2: Using popitem() with only one element in a dictionary

1
2
3
4
5
6
7
cars = {"BMW": 1}

print("Removed item is:", cars.popitem())
print("New Dictionary is:", cars)

# This call will raise a KeyError since the dictionary is now empty
print("Removed item is:", cars.popitem())

Output:

1
2
3
4
5
Removed item is: ('BMW', 1)
New Dictionary is: {}
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
KeyError: 'popitem(): dictionary is empty'

As you can see, calling popitem() on an empty dictionary raises a KeyError exception. It is important to check if the dictionary has items before calling popitem() to avoid this error in production code.


Example 3: Using popitem() in a loop to process all items

A common real-world use case is to drain a dictionary by processing each item one at a time:

1
2
3
4
5
6
7
8
9
10
11
12
13
inventory = {
    "apples": 50,
    "bananas": 30,
    "cherries": 20,
    "dates": 10
}

print("Processing inventory items:")
while inventory:
    item, quantity = inventory.popitem()
    print(f"  Processed: {item} -> Quantity: {quantity}")

print("All items processed. Inventory:", inventory)

Output:

1
2
3
4
5
6
Processing inventory items:
  Processed: dates -> Quantity: 10
  Processed: cherries -> Quantity: 20
  Processed: bananas -> Quantity: 30
  Processed: apples -> Quantity: 50
All items processed. Inventory: {}

This pattern is very handy when you want to consume a dictionary completely while performing some action on each key-value pair. The while inventory: loop automatically stops when the dictionary becomes empty.


Real-World Use Cases of popitem()

1. Implementing a Stack-like Data Structure with a Dictionary

Since popitem() follows LIFO order, you can simulate a simple stack using a dictionary where each key acts as a label and the value holds the data:

1
2
3
4
5
6
7
8
9
task_stack = {}
task_stack["task_1"] = "Download files"
task_stack["task_2"] = "Process data"
task_stack["task_3"] = "Upload results"

# Process tasks in reverse order (LIFO)
while task_stack:
    task_id, task_desc = task_stack.popitem()
    print(f"Executing {task_id}: {task_desc}")

Output:

1
2
3
Executing task_3: Upload results
Executing task_2: Process data
Executing task_1: Download files

2. Undo Feature in Applications

popitem() is useful in scenarios where you need an undo mechanism. For example, storing a history of changes in a dictionary and undoing the most recent change:

1
2
3
4
5
6
7
8
9
10
history = {
    "step_1": "Created file",
    "step_2": "Added header",
    "step_3": "Added content"
}

# Undo the last action
last_action = history.popitem()
print(f"Undoing: {last_action[1]}")
print("Remaining history:", history)

Output:

1
2
Undoing: Added content
Remaining history: {'step_1': 'Created file', 'step_2': 'Added header'}

Common Mistakes When Using popitem()

Mistake 1: Calling popitem() on an empty dictionary

1
2
empty_dict = {}
empty_dict.popitem()  # Raises KeyError!

Fix: Always check if the dictionary is non-empty before calling popitem():

1
2
3
4
if empty_dict:
    empty_dict.popitem()
else:
    print("Dictionary is already empty.")

Mistake 2: Passing arguments to popitem()

1
2
data = {"a": 1, "b": 2}
data.popitem("a")  # TypeError: popitem() takes no arguments

Fix: Call it without any arguments — data.popitem().

Mistake 3: Expecting to choose which item gets removed

popitem() always removes the last inserted item. You cannot specify which key-value pair to remove using popitem(). If you need to remove a specific key, use pop(key) instead.


Tips for Using popitem() Effectively

  • Use popitem() over pop() when you do not care which specific key you remove and just want to process items from the end.
  • Combine with while loops to safely drain an entire dictionary without manually tracking keys.
  • Store the return value if you need the removed key or value for further processing: key, value = my_dict.popitem().
  • Python version matters: In Python 3.7+, popitem() reliably removes the last inserted item. In older versions, behavior is arbitrary.
  • Thread safety: If multiple threads modify a dictionary, consider using locks to prevent race conditions when calling popitem().

Rules of popitem()

  • The popitem() method works on LIFO order (Last In, First Out).
  • It will remove the last key-value pair from the dictionary.
  • Throws a KeyError when no item is left in the dictionary.
  • It takes no parameters; passing arguments raises a TypeError.
  • Returns the removed pair as a tuple (key, value).

Frequently Asked Questions (FAQ)

Q1: What is the difference between pop() and popitem() in Python dictionaries?

pop(key) removes a specific key-value pair identified by the given key and returns the value. popitem() removes and returns the last inserted key-value pair as a (key, value) tuple without requiring you to specify a key. Use pop() when you know the key you want to remove, and use popitem() when you want to remove any item (specifically the last one) without specifying a key.

Q2: Does popitem() work the same in Python 2 and Python 3?

No. In Python 2, dictionaries did not maintain insertion order, so popitem() removed an arbitrary item. In Python 3.7 and later, dictionaries maintain insertion order, so popitem() always removes the last inserted item consistently. If you are writing code for Python 3.7+, you can rely on LIFO ordering with popitem().

Q3: How do I safely use popitem() without risking a KeyError?

Wrap the call in a conditional check or use a try-except block:

1
2
3
4
5
6
7
8
9
10
11
my_dict = {"x": 10}

# Method 1: Check before calling
if my_dict:
    item = my_dict.popitem()

# Method 2: Handle the exception
try:
    item = my_dict.popitem()
except KeyError:
    print("Dictionary is empty, nothing to remove.")

Both approaches ensure your program does not crash when popitem() is called on an empty dictionary.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.