object-properties
list-search
value-matching
data-structures
programming-tutorial

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.

python
1class User:
2    def __init__(self, name, email):
3        self.name = name
4        self.email = email
5
6
7users = [
8    User("Ada", "[email protected]"),
9    User("Grace", "[email protected]"),
10    User("Linus", "[email protected]"),
11]
12
13target = "[email protected]"
14exists = any(user.email == target for user in users)
15
16print(exists)

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.

javascript
1const users = [
2  { name: "Ada", email: "[email protected]" },
3  { name: "Grace", email: "[email protected]" },
4  { name: "Linus", email: "[email protected]" }
5];
6
7const target = "[email protected]";
8const exists = users.some(user => user.email === target);
9
10console.log(exists);

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5public class User
6{
7    public string Name { get; set; }
8    public string Email { get; set; }
9}
10
11public class Program
12{
13    public static void Main()
14    {
15        var users = new List<User>
16        {
17            new User { Name = "Ada", Email = "[email protected]" },
18            new User { Name = "Grace", Email = "[email protected]" },
19            new User { Name = "Linus", Email = "[email protected]" }
20        };
21
22        bool exists = users.Any(user => user.Email == "[email protected]");
23        Console.WriteLine(exists);
24    }
25}

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.

python
emails = {user.email for user in users}
print("[email protected]" in emails)

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:

python
target = "[email protected]".lower()
exists = any(user.email.lower() == target for user in users)

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(), or Any() 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.

Course illustration
Course illustration

All Rights Reserved.