Checking if a list of objects contains a property with a specific value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether any object in a list has a property with a specific value is a standard search problem. The cleanest solution is usually a predicate-based lookup that stops as soon as it finds a match. The exact syntax changes by language, but the underlying idea stays the same: inspect each object until the condition becomes true.
The basic pattern
The problem can be expressed as:
- You have a collection of objects
- Each object has a property
- You want to know whether at least one object has a target value for that property
The simplest form is a linear scan. That is often enough because the code is readable and short-circuits as soon as a match is found.
Python with any()
In Python, any() is the most direct tool for this job.
any() stops at the first True, so it avoids scanning the rest of the list once a match is found.
JavaScript with some()
In JavaScript, Array.prototype.some() plays the same role.
This is the idiomatic approach when you only care about whether a match exists, not about retrieving all matches.
C# with Any()
In C#, LINQ gives you Any() for the same purpose.
Again, the important behavior is short-circuiting. Any() stops once it finds the first match.
When repeated lookups need a different structure
If you perform this check once in a while, a linear scan is fine. If you perform it thousands of times, repeatedly scanning the list becomes wasteful. In that case, build a lookup structure such as a set or map keyed by the property value.
That shifts the cost from repeated search into one preprocessing step, which is often the right tradeoff for frequent membership checks.
Match semantics matter
Be explicit about what "specific value" means in your case:
- Exact match or case-insensitive match
- Null-safe comparison or not
- First match only or all matches
For example, email addresses may need normalization before comparison:
The search API can be correct while the comparison rule is still wrong for your domain.
Common Pitfalls
The most common mistake is using a method that returns all matches when you only need a boolean. any(), some(), and Any() are simpler and usually faster for this question.
Another issue is forgetting about null or missing properties. If the property can be absent, add the necessary guard or normalization before comparing.
Case sensitivity is another frequent trap. Two strings that look equivalent to a user may not compare equal in code unless you normalize them.
Finally, if the same lookup happens repeatedly, stop scanning the list each time. Build a set or dictionary once and use that for fast membership checks.
Summary
- Use predicate-based search methods such as
any(),some(), orAny()for existence checks. - These methods short-circuit when they find the first match.
- Normalize values if matching rules are case-insensitive or format-sensitive.
- Handle null or missing properties explicitly when necessary.
- For repeated lookups, build a keyed structure instead of rescanning the list every time.

