Find dictionary matching value in list - Python
Last Updated :
09 May, 2025
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.
Related Articles
Similar Reads
Python | Find dictionary matching value in list 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
3 min read
Python | Substring Key match in dictionary Sometimes, while working with dictionaries, we might have a use case in which we are not known the exact keys we require but just a specific part of keys that we require to fetch. This kind of problem can arise in many applications. Let's discuss certain ways in which this problem can be solved. Met
9 min read
Python | Find mismatch item on same index in two list Given two list of integers, the task is to find the index at which the element of two list doesn't match. Input: Input1 = [1, 2, 3, 4] Input2 = [1, 5, 3, 6] Output: [1, 3] Explanation: At index=1 we have 2 and 5 and at index=3 we have 4 and 6 which mismatches. Below are some ways to achieve this tas
4 min read
Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp
2 min read
Python - Common list elements and dictionary values Given list and dictionary, extract common elements of List and Dictionary Values. Input : test_list = ["Gfg", "is", "Best", "For"], subs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", } Output : ['Gfg'] Explanation : "Gfg" is common in both list and dictionary value. Input : test_list = ["Gfg", "is",
3 min read
Python - Tuple key detection from value list Sometimes, while working with record data, we can have a problem in which we need to extract the key which has matching value of K from its value list. This kind of problem can occur in domains that are linked to data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
6 min read
Python - Test rear digit match in all list elements Sometimes we may face a problem in which we need to find a list if it contains numbers ending with the same digits. This particular utility has an application in day-day programming. Letâs discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can app
6 min read
Python | Search in Nth column in list of tuples Sometimes, while working with Python list, we can have a data set that consists of tuples and we have a problem in which we need to search the element in the Nth column of list. This has it's applications in web development domain. Let's discuss certain ways in which this task can be performed. Meth
7 min read
Python - Ways to find indices of value in list In Python, it is common to locate the index of a particular value in a list. The built-in index() method can find the first occurrence of a value. However, there are scenarios where multiple occurrences of the value exist and we need to retrieve all the indices. Python offers various methods to achi
3 min read
Python - Test Record existence in Dictionary Sometimes while working with a pool of records, we can have problems in which we need to check the presence of a particular value of a key for existence. This can have applications in many domains such as day-day programming or web development. Let us discuss certain ways in which this task can be p
8 min read