Generating a random number has many application in daily life. In a list, various functions are supported for the same. A whole library is dedicated in Python to handle random numbers. But sometimes, we need to perform a similar task with a dictionary.
popitem() method in dictionary helps to achieve similar purpose. It removes the arbitrary key-value pair from the dictionary and returns it as a tuple. There is an update for this method from Python version 3.7 onwards that it removes the last inserted key-value pair from the dictionary and returns it as a tuple.
Syntax : dict.popitem()
Parameters : None
Returns : A tuple containing the arbitrary key-value pair from dictionary. That pair is removed from dictionary.
Code #1 : Demonstrating the use of popitem()
# Python 3 code to demonstrate # working of popitem() # initializing dictionary test_dict = { "Nikhil" : 7, "Akshat" : 1, "Akash" : 2 } # Printing initial dict print ("The dictionary before deletion : " + str(test_dict)) # using popitem() to return + remove arbitrary # pair res = test_dict.popitem() # Printing the pair returned print ('The arbitrary pair returned is : ' + str(res)) # Printing dict after deletion print ("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.
Code #2 : Demonstrating application of popitem()
# Python 3 code to demonstrate # application of popitem() # initializing dictionary test_dict = { "Nikhil" : 7, "Akshat" : 1, "Akash" : 2 } # Printing initial dict print ("The dictionary before deletion : " + str(test_dict)) n = len(test_dict) # using popitem to assign ranks for i in range(0, n) : print ("Rank " + str(i + 1) + " " + str(test_dict.popitem())) # Printing end dict print ("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 : {}
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python Dictionary | fromkeys() method
- Python Dictionary | setdefault() method
- Python Dictionary | update() method
- Python Dictionary | items() method
- Python Dictionary | keys() method
- Python Dictionary | pop() method
- Use get() method to create a dictionary in Python from a list of elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mkumarchaudhary06

