dict.popitem() Pop arbitrary pair
Last updated: 2026-09-22
dict.popitem() Pop arbitrary pair
dict.popitem() removes and returns an arbitrary (key, value) pair; raises KeyError if empty.
| Category | Dict Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
dict.popitem()
Returns:The removed (key, value) tuple (LIFO order).
Mutates the original:Yes (in place)
💥 Raises
KeyError— Raised when the dict is empty.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
('b', 2)
{'a': 1}
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhich key-value pair does popitem() pop?
APython 3.7+ pops the last-inserted pair in LIFO order; on 3.6 and earlier it is an arbitrary item.
QWhat happens when called on an empty dict?
AA KeyError: 'popitem(): dictionary is empty' is raised.
QWhat does it return?
AA (key, value) tuple, while also removing that item from the dict.
QWhat are common use cases?
AConsuming dict items one by one, or leveraging insertion order to implement a simple LRU cleanup.