How does popitem() work in Python?

Python’s in-built popitem() method is used to remove the last inserted element in a dictionary and return that element as a tuple.

Syntax

dict.popitem()

Parameter and return value

The popitem() method does not require any parameters. In addition, it returns the value that was removed from the dictionary as a key:value pair in a tuple.

Example

postcodes = dict(griffith=2603, belconnen=2617, phillip=2606)
last_item = postcodes.popitem()
print(postcodes)
print(last_item)

Free Resources