Find object in list that has attribute equal to some value that meets any condition
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Searching a list of Python objects by attribute is a very common task, especially when the match is based on a rule instead of a fixed literal. The cleanest approach is usually to combine getattr, a predicate function, and next so you can express the condition directly and stop at the first match.
Finding the first matching object
Assume you have a list of records and want the first object whose status is "open" and whose priority is at least 3.
next(..., None) returns the first matching object or None if nothing matches. This is better than building a whole temporary list when you only need one result.
Using a reusable predicate
When the condition changes often, move it into a function. That keeps the search logic readable and easy to test.
This pattern scales well when the condition includes multiple fields, date checks, or membership tests.
Searching by a dynamic attribute name
Sometimes the attribute itself is chosen at runtime. In that case, getattr is the right tool.
Here, the caller decides both which attribute to inspect and what counts as a valid value. That is a flexible approach for filters in APIs, search utilities, or command-line tools.
Matching with any for more complex conditions
If the attribute is itself a collection, any often reads better than nested loops. Imagine each object has a tags field and you want the first ticket with any tag starting with "prod-".
That combines object-level filtering with attribute-level rules in a single expression.
When you need all matches instead of one
If you want every matching object, use a list comprehension instead of next:
Use next when the first match is enough. Use a list comprehension when the full set matters.
Common Pitfalls
The biggest mistake is forgetting the default argument to next. Without it, a missing result raises StopIteration, which is often not what you want in application code.
Another common issue is accessing attributes that may not exist. getattr(item, "priority") raises AttributeError unless you guard it with hasattr or pass a default value.
Be careful with truthy and falsy values. If the attribute may legitimately be 0, False, or an empty string, do not write logic that treats all falsy values as missing.
Finally, avoid filter plus lambda when it makes the condition harder to read. Python supports it, but a generator expression is usually clearer for object searches.
Summary
- Use
next((item for item in items if condition), None)to return the first matching object. - Move the condition into a helper function when the rule is reused or complex.
- Use
getattrwhen the attribute name is dynamic. - Combine
anywith generator expressions for collection-based attribute checks. - Choose
nextfor a single result and a list comprehension when you need all matches.

