Get random key and value from a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

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

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

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

Get a random key from a dictionary in Python #

To get a random key from a dictionary:

  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

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 #

To get a random value from a dictionary:

  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

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.

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.