Python Dictionary popitem

Python popitem is one of the Dictionary functions, used to remove the last inserted key value pair, and prints the removed pair. In this section, we discuss how to use this Python Dictionary popitem function and the syntax is:

dictionary_name.popitem()

Python Dictionary popitem Example

The popitem function removes the last inserted key value pair and prints the removed items.

TIP: Please refer to the Dictionary in Python.

myDict = {1: 'apple', 2: 'Banana' , 3: 'Orange', 4: 'Kiwi'}
print("Dictionary Items: ", myDict)

# Pop Items
print("\nRemoved Item      :  ",  myDict.popitem())
print("Dictionary Items  :  ",  myDict)

# Add New Item
myDict[5] = 'Mango'
print("\nDictionary Items: ", myDict)

# Pop Items
print("\nRemoved Item      :  ",  myDict.popitem())
print("Dictionary Items  :  ",  myDict)
Python Dictionary popitem Example