Print all values of a given key in a List of Dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Print all values of a given key in a List of Dictionaries in Python #

To print all values of a given key in a list of dictionaries:

  1. Use a list comprehension to iterate over the list.
  2. Access the specific key in each dictionary and return the result.
  3. Use the print() function to print all values of the key.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3, 'name': 'Carl'}, ] result = [dictionary['name'] for dictionary in list_of_dictionaries] print(result) # 👉️ ['Alice', 'Bobbyhadz', 'Carl'] for dictionary in list_of_dictionaries: # Alice # Bobbyhadz # Carl print(dictionary['name'])

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 value.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3, 'name': 'Carl'}, ] result = [dictionary['name'] for dictionary in list_of_dictionaries] print(result) # 👉️ ['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.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3}, ] result = [dictionary['name'] for dictionary in list_of_dictionaries if 'name' in dictionary] print(result) # 👉️ ['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.

Print all values of a given key in a List of Dictionaries using for loop #

To print all values of a given key in a list of dictionaries:

  1. Use a for loop to iterate over the list.
  2. Use bracket notation to access the specified key.
  3. Use the print() function to print the result.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3, 'name': 'Carl'}, ] for dictionary in list_of_dictionaries: # Alice # Bobbyhadz # Carl print(dictionary['name'])

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

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'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3}, ] for dictionary in list_of_dictionaries: # Alice # Bobbyhadz if 'name' in dictionary: print(dictionary['name'])

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.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bobbyhadz'}, {'id': 3}, ] for dictionary in list_of_dictionaries: # Alice # Bobbyhadz # None print(dictionary.get('name'))

If you need to get a list containing the values of the key, use the list.append() method.

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

The list.append() method adds an item to the end of 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.