Get a list of values from a list of dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Get a list of values from a list of dictionaries in Python #

To get a list of values from a list of dictionaries:

  1. Use a list comprehension to iterate over the list.
  2. Use bracket notation to get the value of the current key.
  3. The new list will contain the values of the specified key.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'name': 'carl', 'task': 'ship'}, ] values = [dictionary['name'] for dictionary in list_of_dictionaries] print(values) # 👉️ ['alice', 'bobbyhadz', 'carl'] # ----------------------------------------------------- # 👇️ if some of the dictionaries might not contain the key values = [dictionary['name'] for dictionary in list_of_dictionaries if 'name' in dictionary] print(values) # 👉️ ['alice', 'bobbyhadz', 'carl']

We used a list comprehension to iterate over the list of dictionaries.

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 given key in the dictionary and return the corresponding value.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'name': 'carl', 'task': 'ship'}, ] values = [dictionary['name'] for dictionary in list_of_dictionaries] print(values) # 👉️ ['alice', 'bobbyhadz', 'carl']

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

If some of the dictionaries in the list might not have the given key, use an if statement in the list comprehension.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'task': 'ship'}, ] values = [dictionary['name'] for dictionary in list_of_dictionaries if 'name' in dictionary] print(values) # 👉️ ['alice', 'bobbyhadz']

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

When using this approach, we won't get a KeyError even if some of the dictionaries in the list don't contain the key.

Alternatively, you can use a for loop.

Get a list of values from a list of dictionaries using a for loop #

To get a list of values from a list of dictionaries:

  1. Use a for loop to iterate over the list.
  2. Use the list.append() method to append the values of the key to a new list.
  3. The new list will only contain the values of the specified key.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'name': 'carl', 'task': 'ship'}, ] values = [] for dictionary in list_of_dictionaries: values.append(dictionary['name']) print(values) # 👉️ ['alice', 'bobbyhadz', 'carl']

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

On each iteration, we use the list.append() method to add the value of the key 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']
If the key might not exist in some of the dictionaries, use an if statement before accessing it.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'task': 'ship'}, ] values = [] for dictionary in list_of_dictionaries: if 'name' in dictionary: values.append(dictionary['name']) print(values) # 👉️ ['alice', 'bobbyhadz']

When using bracket notation to access a key that doesn't exist, a KeyError is raised.

There is also a dict.get() method that returns None for non-existent keys by 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.

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

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'alice', 'task': 'develop'}, {'id': 2, 'name': 'bobbyhadz', 'task': 'test'}, {'id': 3, 'task': 'ship'}, ] values = [] for dictionary in list_of_dictionaries: values.append(dictionary.get('name', 'default value')) print(values) # 👉️ ['alice', 'bobbyhadz', 'default value']
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.