Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How does popitem() work in Python?

Hassan Ahmed

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)

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring