Dict Methods

dict.popitem()

Removes the most recently inserted remaining entry and returns its key together with its value. Updating an existing key does not move it to the end; deleting and reinserting a key does. This LIFO behavior is guaranteed from Python 3.7. An empty dictionary raises KeyError. Useful for destructive processing when insertion order is meaningful, not for selecting an arbitrary random pair.

Formal Syntax & Structure

python syntaxpython
mapping.popitem()

Return Value

tuple

Two-element tuple containing the removed key and value.

Working Example

Example Snippetpython
jobs = {"build": 1, "test": 2, "deploy": 3}jobs["build"] = 4print(jobs.popitem())print(jobs.popitem())print(jobs)
Python Reference