Home / Python / Reference / Dictionary methods

Python dictionary popitem() method

Remove and return the most recently added (key, value) pair.

Syntax

dict.popitem()

Raises KeyError if the dictionary is empty.

Parameters

None.

Return value

a (key, value) tuple

Example

Code

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

Output

('c', 3) {'a': 1, 'b': 2}

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.