Python popitem Dictionary is one of a Dictionary function, used to remove last inserted key value pair, and prints the removed pair. In this article, we will show you, How to use this Python Dictionary popitem function with practical examples.
TIP: Please refer Dictionary article to understand everything about Dictionaries.
Python Dictionary popitem Syntax
The basic syntax behind this popitem function is:
dictionary_name.popitem()
Python Dictionary popitem Example
The python popitem dictionary function removes the last inserted key value pair and prints the removed items.
# 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)
OUTPUT