Python Dictionary popitem() method
Python dictionary popitem() method removes the last inserted key-value pair from the dictionary and returns it as a tuple.
Syntax : dict.popitem()
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Parameters : None
Returns : A tuple containing the arbitrary key-value pair from dictionary. That pair is removed from dictionary.
Generating a random number has many applications in daily life. In a list, various functions are supported for the same. A whole library is dedicated to Python to handle random numbers. But sometimes, we need to perform a similar task with a dictionary.
Note: popitem() method return keyError if dictionary is empty.
Python Dictionary popitem() method Example
Example #1: Demonstrating the use of popitem()
Here we are going to use python dict to popitem at last element.
Python3
# Python 3 code to demonstrate# working of popitem()# initializing dictionarytest_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}# Printing initial dictprint("The dictionary before deletion : " + str(test_dict))# using popitem() to return + remove arbitrary# pairres = test_dict.popitem()# Printing the pair returnedprint('The arbitrary pair returned is : ' + str(res))# Printing dict after deletionprint("The dictionary after removal : " + str(test_dict)) |
Output :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
The arbitrary pair returned is : ('Akash', 2)
The dictionary after removal : {'Nikhil': 7, 'Akshat': 1}Practical Application: This particular function can be used to formulate the random name for playing a game or deciding the random ranklist without using any random function.
Example #2: Demonstrating the application of popitem()
Python3
# Python 3 code to demonstrate# application of popitem()# initializing dictionarytest_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}# Printing initial dictprint("The dictionary before deletion : " + str(test_dict))n = len(test_dict)# using popitem to assign ranksfor i in range(0, n): print("Rank " + str(i + 1) + " " + str(test_dict.popitem()))# Printing end dictprint("The dictionary after deletion : " + str(test_dict)) |
Output :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Rank 1 ('Akash', 2)
Rank 2 ('Akshat', 1)
Rank 3 ('Nikhil', 7)
The dictionary after deletion : {}Example #3: Python Dictionary popitem random
Python3
# Python3 code to demonstrate working of# Get random dictionary pair in dictionary# Using popitem()# Initialize dictionarytest_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Get random dictionary pair in dictionary# Using popitem()res = test_dict.popitem()# printing resultprint("The random pair is : " + str(res)) |
Output:
The original dictionary is : {'Gfg': 1, 'best': 3, 'is': 2}
The random pair is : ('is', 2)
