Get list of dictionary Values from list of Keys in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Get list of dictionary Values from list of Keys in Python #

To get a list of dictionary values from a list of keys:

  1. Use a list comprehension to iterate over the list of keys.
  2. Use bracket notation to get the value of each key.
  3. The list will only contain the values of the specified keys.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'site': 'bobbyhadz.com', 'language': 'Python' } keys = ['name', 'language'] result = [my_dict[key] for key in keys] print(result) # 👉️ ['Borislav Hadzhiev', 'Python'] # 👇️ if some keys might not exist in the dictionary result = [my_dict[key] for key in keys if key in my_dict] print(result) # 👉️ ['Borislav Hadzhiev', 'Python']

We used a list comprehension to iterate over the list of 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 use bracket notation to get the value of the current key and return the result.

If some of the keys in the list might not exist in the dictionary, use an if statement.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'site': 'bobbyhadz.com', 'language': 'Python' } keys = ['name', 'language', 'another'] result = [my_dict[key] for key in keys if key in my_dict] print(result) # 👉️ ['Borislav Hadzhiev', 'Python']

The if statement checks if the key exists in the dictionary before accessing it.

This way we won't get a KeyError even if some of the keys in the list are not present in the dictionary.

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

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'age': 30, 'site': 'bobbyhadz.com', 'language': 'Python' } keys = ['name', 'language', 'another'] result = [my_dict.get(key) for key in keys] print(result) # 👉️ ['Borislav Hadzhiev', 'Python', None] # 👇️ specified default value for non-existent keys result = [my_dict.get(key, 'default') for key in keys] print(result) # 👉️ ['Borislav Hadzhiev', 'Python', 'default']

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.

Alternatively, you can use a for loop.

Get list of dictionary Values from list of Keys using for loop #

To get a list of dictionary values from a list of keys:

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

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

On each iteration, we use the list.append() method to append the value of the current key to a new list.

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

Use an if statement if some of the keys in the list might not exist in the dictionary.

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

The in operator checks if the key is present in the dictionary before appending its value to 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.