Get multiple values from a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Get multiple values from a dictionary in Python #

To get multiple values from a dictionary:

  1. Use a list comprehension to iterate over the collection of keys.
  2. Access each key and return the corresponding value.
  3. The new list will contain the values of the specified keys.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site'] values = [my_dict[key] for key in keys] print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com'] # ---------------------------------------- # 👇️ using dict.get() values = [my_dict.get(key) for key in keys] print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com']

We used a list comprehension to iterate over the collection of dictionary keys.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the current key and return the corresponding value.

The new list only contains the values of the specified keys.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site'] values = [my_dict[key] for key in keys] print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com']
If you use bracket notation to access a key that doesn't exist in the dictionary a KeyError exception is raised.

You can use an if statement to check if the key is present in the dictionary before accessing it.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site', 'another', 'example'] values = [my_dict[key] for key in keys if key in my_dict] print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com']

We only use bracket notation to access the key if it exists in the dictionary.

This way, we won't get a KeyError exception even if some of the keys don't exist.

There is also a dict.get() method. The method returns None for non-existent keys by default.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site', 'another'] values = [my_dict.get(key) for key in keys] # 👇️ ['Borislav Hadzhiev', 'bobbyhadz.com', None] print(values)

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)
If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

If you need to specify a default value for non-existent keys, pass a second argument in the call to the dict.get() method.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site', 'another'] values = [my_dict.get(key, 'default value') for key in keys] # 👇️ ['Borislav Hadzhiev', 'bobbyhadz.com', 'default value'] print(values)

Alternatively, you can use a for loop.

Get multiple values from a dictionary using a for loop #

To get multiple values from a dictionary:

  1. Use a for loop to iterate over the collection of keys.
  2. Use the list.append() method to append the value of each key to a list.
  3. The new list will only contain the values of the specified keys.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site', 'another', 'example'] values = [] for key in keys: if key in my_dict: values.append(my_dict[key]) print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com']

We used a for loop to iterate over the collection of keys.

On each iteration, we check if the key is present in the dictionary and use the list.append() method to add its value to a new list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', 'com']

Alternatively, you can use the dict.get() method to access each key.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'topic': 'Python' } keys = ['name', 'site', 'another', 'example'] values = [] for key in keys: values.append(my_dict.get(key)) print(values) # 👉️ ['Borislav Hadzhiev', 'bobbyhadz.com', None, None]

Make sure to pass a second argument to the dict.get() method if you need to specify a default value for non-existent keys.

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.