Find dictionary in a List of dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Find dictionary in a List of dictionaries in Python
  2. Find all dictionaries in a List that meet a condition

Find dictionary in a List of dictionaries in Python #

To find a dictionary in a list of dictionaries:

  1. Use a generator expression to iterate over the list.
  2. Check if each dictionary has a key that meets a condition.
  3. Use the next() function to return the first matching dictionary.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] # ✅ find dictionary in list of dictionaries result = next( (item for item in list_of_dictionaries if item['name'] == 'Bobbyhadz'), None ) print(result) # 👉️ {'id': 2, 'name': 'Bobbyhadz', 'salary': 100} # -------------------------------------------------------- # ✅ find all dictionaries in list that meet a condition result = [ item for item in list_of_dictionaries if item['salary'] == 100 ] # 👉️ [{'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}] print(result)
The code sample finds the first dictionary in the list that meets a condition. If you need to find all dictionaries that meet a certain condition in a list, scroll down to the next subheading.

We used a generator expression to iterate over the list of dictionaries.

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

On each iteration, we check if the current dictionary has a name key with a specific value and return the result.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] result = next( (item for item in list_of_dictionaries if item['name'] == 'Bobbyhadz'), None ) print(result) # 👉️ {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}

When we pass an iterator to the next() function, the next item in the stream is returned.

We also specified a None default value, which is returned if none of the dictionaries in the list meet the condition.

If some of the dictionaries in the list might not have the key you are checking for, use the dict.get() method.

main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] result = next( (item for item in list_of_dictionaries if item.get('name') == 'Bobbyhadz'), None ) print(result) # 👉️ {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}

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 simple for loop.

To find a dictionary in a list of dictionaries:

  1. Use a for loop to iterate over the list.
  2. Check if each dictionary has a key that meets a condition.
  3. Use the break statement to break out of the loop once you find a matching dictionary.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] match = None for item in list_of_dictionaries: if item.get('name') == 'Bobbyhadz': match = item break print(match) # 👉️ {'id': 2, 'name': 'Bobbyhadz', 'salary': 100} if match is not None: print(match['id']) # 👉️ 2 print(match['name']) # 👉️ Bobbyhadz

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

Once we find a matching dictionary, we assign it to the match variable and break out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

Find all dictionaries in a List that meet a condition #

To find all dictionaries in a list that meet a condition:

  1. Use a list comprehension to iterate over the list.
  2. Check if each dictionary has a key that meets a condition.
  3. The new list will only contain the matching dictionaries.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] result = [ item for item in list_of_dictionaries if item['salary'] == 100 ] # 👉️ [{'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}] print(result)

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 check if the current dictionary has a salary key with a value of 100 and return the result.

Alternatively, you can use a for loop.

To find all dictionaries in a list that meet a condition:

  1. Use a for loop to iterate over the list.
  2. Check if each dictionary has a key that meets a condition.
  3. Append the matching dictionaries to a new list.
main.py
list_of_dictionaries = [ {'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}, {'id': 3, 'name': 'Carl', 'salary': 200}, ] matches = [] for item in list_of_dictionaries: if item['salary'] == 100: matches.append(item) # 👇️ [{'id': 1, 'name': 'Alice', 'salary': 100}, {'id': 2, 'name': 'Bobbyhadz', 'salary': 100}] print(matches)

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

On each iteration, we check if the current dictionary has a salary key with a value of 100 and if the condition is met, we append the dictionary to the matches 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.