Last updated: Apr 9, 2024
Reading timeยท4 min
To get a random key-value pair from a dictionary:
dict.items()
method to get a view of the dictionary's items.list()
class to convert the view to a list.random.choice()
method to get a random key-value pair from the
dictionary.import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # โ get random key-value pair from dictionary key, value = random.choice(list(my_dict.items())) print(key, value) # ๐๏ธ name Borislav Hadzhiev # ----------------------------------------------- # โ get random key from dictionary key = random.choice(list(my_dict)) print(key) # ๐๏ธ topic # ----------------------------------------------- # โ get random value from dictionary value = random.choice(list(my_dict.values())) print(value) # ๐๏ธ bobbyhadz.com
The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).
my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # ๐๏ธ dict_items([('name', 'Borislav Hadzhiev'), ('fruit', 'apple'), ('number', 5), ('website', 'bobbyhadz.com'), ('topic', 'Python')]) print(my_dict.items())
list()
class to convert the view of key-value pairs to a list before passing it to the random.choice()
method.The list class takes an iterable and returns a list object.
The random.choice() method takes a sequence and returns a random element from the non-empty sequence.
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } key, value = random.choice(list(my_dict.items())) print(key, value) # ๐๏ธ website bobbyhadz.com
If the sequence is empty, the random.choice()
method raises an IndexError
.
This is a two-step process:
list()
class to convert the dictionary to a list of keys.random.choice()
method to get a random key from the list.import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } key = random.choice(list(my_dict)) print(key) # ๐๏ธ topic
We used the list()
class to convert the dictionary to a list of keys.
my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # ๐๏ธ ['name', 'fruit', 'number', 'website', 'topic'] print(list(my_dict)) # ๐๏ธ ['name', 'fruit', 'number', 'website', 'topic'] print(list(my_dict.keys()))
dict.keys()
method to be more explicit.The dict.keys() method returns a new view of the dictionary's keys.
The last step is to pass the list of keys to the random.choice()
method to get
a random key.
This is a three-step process:
dict.values()
method to get a view of the dictionary's values.list()
class to convert the view object to a list.random.choice()
method to get a random value from the dictionary.import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } value = random.choice(list(my_dict.values())) print(value) # ๐๏ธ Borislav Hadzhiev
The dict.values() method returns a new view of the dictionary's values.
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # ๐๏ธ dict_values(['Borislav Hadzhiev', 'apple', 5, 'bobbyhadz.com', 'Python']) print(my_dict.values())
We used the list()
class to convert the view object to a list and used the
random.choice()
method to get a random value from the list.
If you need to get multiple random keys and values from a dictionary, use the
random.sample()
method.
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.sample(list(my_dict), 2) print(random_keys) # ๐๏ธ ['age', 'name'] random_values = random.sample(list(my_dict.values()), 2) print(random_values) # ๐๏ธ [30, 'Bobbyhadz'] random_items = random.sample(list(my_dict.items()), 2) print(random_items) # ๐๏ธ [('country', 'Chile'), ('id', 1)]
The random.sample() method returns a list of N unique elements chosen from the provided sequence.
Notice that we used the list()
class to convert the dictionary's keys, values
and items to lists in the call to random.sample()
.
We can't directly pass a dictionary to the random.sample()
method.
You can also use the random.choices()
method to get multiple random keys and
values from a dictionary.
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.choices(list(my_dict), k=2) print(random_keys) # ๐๏ธ ['name', 'country'] random_values = random.choices(list(my_dict.values()), k=2) print(random_values) # ๐๏ธ ['Santiago', 30] random_items = random.choices(list(my_dict.items()), k=2) print(random_items) # ๐๏ธ [('id', 1), ('city', 'Santiago')]
The
random.choices()
method returns a k
sized list of elements chosen from the provided iterable
with replacement.
If the iterable is empty, the method raises an IndexError
exception.
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.choices(list(my_dict), k=3) print(random_keys) # ๐๏ธ ['city', 'city', 'name'] random_values = random.choices(list(my_dict.values()), k=3) print(random_values) # ๐๏ธ ['Santiago', 1, 'Santiago'] random_items = random.choices(list(my_dict.items()), k=3) # ๐๏ธ [('age', 30), ('name', 'Bobbyhadz'), ('name', 'Bobbyhadz')] print(random_items)
Notice that when using the random.choices()
method it is possible to get
repeated values.
This is not the case when using the random.sample()
method.
The random.sample()
method can never return repeated values.
You can learn more about the related topics by checking out the following tutorials: