Accessing values in a List of Dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Accessing values in a List of Dictionaries in Python #

To access values in a list of dictionaries:

  1. Use a for loop to iterate over the list.
  2. Use bracket notation to access the value of a specific key in each dictionary.
  3. Use the list.append() method to append the values to a list.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'name': 'Carl', 'job': 'cake designer'}, ] values = [] # ✅ Accessing values in list of dictionaries (using for loop) for dictionary in list_of_dictionaries: # Alice # Borislav # Carl print(dictionary['name']) values.append(dictionary['name']) print(values) # 👉️ ['Alice', 'Borislav', 'Carl'] # ------------------------------------------- # ✅ Accessing values in list of dictionaries (using list comprehension) values = [dictionary['name'] for dictionary in list_of_dictionaries] print(values) # 👉️ ['Alice', 'Borislav', 'Carl']

The first example uses a for loop to iterate over the list of dictionaries.

On each iteration, we access a specific key to get the corresponding value.

You can use the list.append() method to append the values of the specified key to a list.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'name': 'Carl', 'job': 'cake designer'}, ] values = [] for dictionary in list_of_dictionaries: # Alice # Borislav # Carl print(dictionary['name']) values.append(dictionary['name']) print(values) # 👉️ ['Alice', 'Borislav', 'Carl']

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']

Use an if statement if the key might not exist in some of the dictionaries in the list.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'job': 'cake designer'}, ] values = [] for dictionary in list_of_dictionaries: if 'name' in dictionary: values.append(dictionary['name']) print(values) # 👉️ ['Alice', 'Borislav']

We used an if statement to check if each dictionary contains the specified key before accessing it.

Alternatively, you can use a list comprehension.

Accessing values in a List of Dictionaries using list comprehension #

To access values in a list of dictionaries:

  1. Use a list comprehension to iterate over the list.
  2. Use bracket notation to access the value of a specific key.
  3. The new list will only contain the values of the specified key.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'name': 'Carl', 'job': 'cake designer'}, ] values = [dictionary['name'] for dictionary in list_of_dictionaries] print(values) # 👉️ ['Alice', 'Borislav', '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 key and return the corresponding value.

If the key might not exist in some of the dictionaries in the list, use an if statement.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'job': 'cake designer'}, ] values = [ dictionary['name'] for dictionary in list_of_dictionaries if 'name' in dictionary] print(values) # 👉️ ['Alice', 'Borislav']

The if statement checks if the name key is present in each dictionary before returning its value.

If we use bracket notation to access a key that doesn't exist, a KeyError exception is raised.

On the other hand, the dict.get() method returns None for non-existent keys by default.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'job': 'cake designer'}, ] values = [ dictionary.get('name') for dictionary in list_of_dictionaries ] print(values) # 👉️ ['Alice', 'Borislav', None]

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 return a default value if the key is not present in a dictionary, pass a second argument to the dict.get() method.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'job': 'accountant'}, {'id': 2, 'name': 'Borislav', 'job': 'beekeeper'}, {'id': 3, 'job': 'cake designer'}, ] values = [ dictionary.get('name', 'default') for dictionary in list_of_dictionaries ] print(values) # 👉️ ['Alice', 'Borislav', 'default']

The name key doesn't exist in the third dictionary in the list, so the dict.get() method returns the provided 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.