Get random Key and Value from a Dictionary in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Get random key and value from a dictionary in Python
  2. Get a random key from a dictionary in Python
  3. Get a random value from a dictionary in Python
  4. Get multiple random keys and values from a Dictionary in Python
  5. Get multiple random keys and values from a Dictionary using random.choices()

# Get random key and value from a dictionary in Python

To get a random key-value pair from a dictionary:

  1. Use the dict.items() method to get a view of the dictionary's items.
  2. Use the list() class to convert the view to a list.
  3. Use the random.choice() method to get a random key-value pair from the dictionary.
main.py
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

get random key and value from dictionary

The code for this article is available on GitHub

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

main.py
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())
We used the 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.

main.py
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

using random choice method

If the sequence is empty, the random.choice() method raises an IndexError.

# Get a random key from a dictionary in Python

This is a two-step process:

  1. Use the list() class to convert the dictionary to a list of keys.
  2. Use the random.choice() method to get a random key from the list.
main.py
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

get random key from dictionary

The code for this article is available on GitHub

We used the list() class to convert the dictionary to a list of keys.

main.py
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()))
We could have also used the 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.

# Get a random value from a dictionary in Python

This is a three-step process:

  1. Use the dict.values() method to get a view of the dictionary's values.
  2. Use the list() class to convert the view object to a list.
  3. Use the random.choice() method to get a random value from the dictionary.
main.py
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

get random value from dictionary in python

The code for this article is available on GitHub

The dict.values() method returns a new view of the dictionary's values.

main.py
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.

# Get multiple random keys and values from a Dictionary in Python

If you need to get multiple random keys and values from a dictionary, use the random.sample() method.

main.py
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)]

get multiple random keys and values from dictionary

The code for this article is available on GitHub

The random.sample() method returns a list of N unique elements chosen from the provided sequence.

The first argument the method takes is a sequence and the second is the number of random elements to be returned.

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.

# Get multiple random keys and values from a Dictionary using random.choices()

You can also use the random.choices() method to get multiple random keys and values from a dictionary.

main.py
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 code for this article is available on GitHub

The random.choices() method returns a k sized list of elements chosen from the provided iterable with replacement.

With replacement basically means that the same element can be returned multiple times.

If the iterable is empty, the method raises an IndexError exception.

main.py
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)
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev