Open In App

Find dictionary matching value in list - Python

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Finding a dictionary with a specific value in a list of dictionaries involves searching through the list and matching a key-value pair in each dictionary. For example, given the list of dictionaries [{‘Course’: “C++”, ‘Author’: “Jerry”}, {‘Course’: “Python”, ‘Author’: “Mark”}], you may want to find the dictionary where the Author is “Mark”. Let’s explore various methods to efficiently achieve this task.

Using generator expression

Generator expression efficiently search through the list, instantly returning the first match that satisfies the condition. It is memory-efficient, as it stops execution as soon as the condition is met, making it highly preferred for its concise syntax and optimal performance, especially when dealing with large datasets.

Python
d = [
    {'Course': "C++", 'Author': "Jerry"},
    {'Course': "Python", 'Author': "Mark"},
    {'Course': "Java", 'Author': "Paul"}
]

res = next((sub for sub in d if sub['Author'] == "Mark"), None)
print(res)

Output
{'Course': 'Python', 'Author': 'Mark'}

Explanation:

  • generator expression iterates over d and yields dictionaries where the Author is "Mark".
  • next() function returns the first match or None if no match is found.

Using filter()

This approach combines filter() and next() to find the first dictionary that meets a condition. It uses a generator under the hood, making it memory-efficient and fast, as it stops at the first match. While the use of lambda can slightly affect readability, it remains a clean, functional and effective solution.

Python
d = [
    {'Course': "C++", 'Author': "Jerry"},
    {'Course': "Python", 'Author': "Mark"},
    {'Course': "Java", 'Author': "Paul"}
]

res = next(filter(lambda x: x['Author'] == "Mark", d), None)
print(res)

Output
{'Course': 'Python', 'Author': 'Mark'}

Explanation:

  • filter() function with a lambda expression filters the dictionaries in d where the Author is "Mark".
  • next() function returns the first match from the filtered result or None if no match is found.

Using list comprehension

This method builds a list of all dictionaries that match the condition and then picks the first one. It's easy to read and commonly used, but less efficient because it checks the entire list, even after finding a match.

Python
d = [
    {'Course': "C++", 'Author': "Jerry"},
    {'Course': "Python", 'Author': "Mark"},
    {'Course': "Java", 'Author': "Paul"}
]

res = [sub for sub in d if sub['Author'] == "Mark"]
res = res[0] if res else None
print(res)

Output
{'Course': 'Python', 'Author': 'Mark'}

Explanation:

  • List comprehension filters d to get all dictionaries where the Author is "Mark".
  • The first match is selected with res[0] if the list is not empty otherwise, None is returned.

Using for loop

This method uses a simple loop to check each dictionary and stops at the first match. It’s explicit, beginner-friendly and very readable, though slightly longer. It’s also easy to debug.

Python
d = [
    {'Course': "C++", 'Author': "Jerry"},
    {'Course': "Python", 'Author': "Mark"},
    {'Course': "Java", 'Author': "Paul"}
]

res = None
for sub in d:
    if sub['Author'] == "Mark":
        res = sub
        break

print(res)

Output
{'Course': 'Python', 'Author': 'Mark'}

Explanation:

  • For loop iterates through d and checks if the Author is "Mark".
  • When a match is found, it assigns the dictionary to res and breaks the loop to stop further checks.

Practice Tags :

Similar Reads