Search a list of dictionaries in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Searching a list of dictionaries is a common task when working with records, API responses, or configuration data in Python. The most Pythonic approach uses a generator expression with next() for single matches or a list comprehension for multiple matches. For frequent lookups, build an index dictionary keyed by the search field. The right approach depends on whether you need one result or many, and how often you search the same data.
Find First Match
Use next() with a generator expression to find the first dictionary matching a condition:
next() stops as soon as the first match is found — it does not scan the entire list.
Find All Matches
Use a list comprehension to find all matching dictionaries:
Search by Partial Match
Build an Index for Fast Lookups
When searching the same list repeatedly, build a dictionary index:
Using filter()
List comprehensions are generally preferred over filter() in Python for readability.
Using operator.itemgetter
Handling Missing Keys
Common Pitfalls
- Using a loop when
next()suffices: Writing aforloop withbreakto find the first match is verbose.next(x for x in items if condition, default)is the idiomatic Python pattern. - Linear search on repeated lookups: Searching a list of 10,000 dictionaries 1,000 times is O(n*m). Build an index dictionary once (O(n)) and use O(1) lookups for each query.
- KeyError on missing fields: Not all dictionaries may have the same keys. Use
u.get("key")instead ofu["key"]to safely handle missing fields, or filter with"key" in ufirst. - Mutating dictionaries during search: Modifying a dictionary while iterating over the list can cause unexpected behavior. Create a new list of results rather than modifying in place.
- Forgetting that
next()without a default raisesStopIteration: If no match is found and no default is provided,next()raisesStopIteration, which can be confusing inside generators or loops. Always provide a default value likeNone.
Summary
- Use
next(x for x in items if cond, None)to find the first matching dictionary - Use list comprehensions
[x for x in items if cond]to find all matches - Build a dictionary index
{d["key"]: d for d in items}for O(1) repeated lookups - Use
.get()instead of[]to safely handle missing keys - Use
sorted()withkey=itemgetter("field")for sorting by a specific key - Prefer list comprehensions over
filter()for readability
Related reading
- Search an element in a heap
- search for interval overlap in list of intervals?
- Search in Rotated Sorted Array in Olog n time
- Searching a tree using LINQ
- Search and replace a line in a file in Python
- Search for does-not-contain on a DataFrame in pandas
- Searching for an element in a circular sorted array
- Searching in a sorted and rotated array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.