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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original dictionary is : {'Gfg': 1, 'best': 3, 'is': 2}
The random pair is : ('is', 2)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.