Function Details: popitem

Description


Removes and return a (key, value) pair as a 2-tuple.


Extended Description


The popitem() method removes and returns an arbitrary (key, value) pair from the dictionary as a tuple. In Python 3.7+, popitem() removes the last inserted item, making dictionaries effectively ordered. If the dictionary is empty, popitem() raises a KeyError. This method is useful when you need to destructively iterate over a dictionary.


Function Signature


dict.popitem() -> Tuple[Any, Any]

Module: dict

Class: dictionary

Parameters



Return


Returns a tuple containing the (key, value) pair that was removed from the dictionary.


Return Type


Tuple

Output

Explanation

This example shows how to use popitem() to remove and return a (key, value) pair from the dictionary.