Python | Get random dictionary pair
Sometimes, while working with dictionaries, we can have a situation in which we need to find a random pair from dictionary. This type of problem can come in games such as lotteries etc. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using random.choice() + list() + items()
The combination of above methods can be used to perform this task. The choice function performs the task of random value selection and list method is used to convert the pairs accessed using items() into a list over which choice function can work.
# Python3 code to demonstrate working of # Get random dictionary pair in dictionary # Using random.choice() + list() + items() import random # Initialize dictionary test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Get random dictionary pair in dictionary # Using random.choice() + list() + items() res = key, val = random.choice(list(test_dict.items())) # printing result print("The random pair is : " + str(res)) |
The original dictionary is : {'Gfg': 1, 'best': 3, 'is': 2}
The random pair is : ('is', 2)
Method #2 : Using popitem()
This function is generally used to remove an item from dictionary and remove it. The logic why this function can be used to perform this task is that as ordering in dictionary is random, any pair can be popped hence random.
# Python3 code to demonstrate working of # Get random dictionary pair in dictionary # Using popitem() # Initialize dictionary test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Get random dictionary pair in dictionary # Using popitem() res = test_dict.popitem() # printing result print("The random pair is : " + str(res)) |
The original dictionary is : {'Gfg': 1, 'best': 3, 'is': 2}
The random pair is : ('is', 2)
Recommended Posts:
- Add a key:value pair to dictionary in Python
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Convert string dictionary to dictionary
- Generating random Id's in Python
- Random Numbers in Python
- random.seed( ) in Python
- SymPy | Permutation.random() in Python
- Random Forest Regression in Python
- numpy.random.rand() in Python
- Python | random.sample() function
- numpy.random.randn() in Python
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.



