Access an arbitrary element in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Access an arbitrary element in a dictionary in Python #

To access an arbitrary element in 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 access an arbitrary item in the dictionary.
main.py
import random my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } # ✅ access arbitrary key-value pair in dictionary key, value = random.choice(list(my_dict.items())) print(key, value) # 👉️ website bobbyhadz.com # ✅ access arbitrary key in dictionary key = random.choice(list(my_dict)) print(key) # 👉️ website # ✅ access arbitrary value in 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', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ dict_items([('name', 'Borislav Hadzhiev'), ('age', 30), ('website', 'bobbyhadz.com'), ('topic', 'Python')]) print(my_dict.items())

We have to use the list() class to convert the view object to a list.

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', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } key, value = random.choice(list(my_dict.items())) print(key, value) # 👉️ name Borislav Hadzhiev

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

Access an arbitrary key in a dictionary in Python #

To access an arbitrary key in a dictionary:

  1. Use the list() class to convert the dictionary to a list of keys.
  2. Use the random.choice() method to get an arbitrary key.
main.py
import random my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } key = random.choice(list(my_dict)) print(key) # 👉️ website

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

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } print(list(my_dict)) # 👉️ ['name', 'age', 'website', 'topic'] print(list(my_dict.keys())) # 👉️ ['name', 'age', 'website', 'topic']

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 an arbitrary key.

Access an arbitrary value in a dictionary in Python #

To access an arbitrary value in 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 to a list.
  3. Use the random.choice() method to access an arbitrary value in the dictionary.
main.py
import random my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, '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', 'age': 30, 'website': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ dict_values(['Borislav Hadzhiev', 30, '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 an arbitrary 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.