Python Dictionary popitem

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

The syntax of this dictionary popitem function is:

dictionary_name.popitem()

Python Dictionary popitem Example

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

TIP: Please refer to the Dictionary in Python.

# Python Dictionary popitem Example

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