Python
programming
lists
contains function
coding tips

Is there a short contains function for lists?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, you usually do not call a contains method on a list. The short and idiomatic way to test membership is the in operator, which reads naturally and is built into the language.

Use the in Operator

For a normal list membership check, write:

python
1numbers = [10, 20, 30, 40]
2
3print(20 in numbers)
4print(99 in numbers)
5print(99 not in numbers)

Output:

text
True
False
True

That is the Python equivalent of a "contains" function. It is short, readable, and the form other Python developers will expect to see.

You can use the same idea with strings, tuples, sets, and dictionaries:

python
print("cat" in ["dog", "cat", "bird"])
print("user_id" in {"user_id": 42, "name": "Ava"})

One detail matters here: for dictionaries, in checks keys, not values.

How Membership Testing Works

For a list, Python checks elements one by one until it finds a match or reaches the end. In practice, that means list membership is linear in the size of the list.

python
1fruits = ["apple", "banana", "orange"]
2
3if "banana" in fruits:
4    print("found it")

That is fine for small and medium lists. It is also fine when the check is rare. But if you are doing thousands of lookups, a list may not be the right data structure.

Python's membership semantics are based on equality, not identity. So this matters:

python
1a = [1, 2]
2b = [1, 2]
3
4print(b in [a])

The output is True because the two lists compare equal. Python is not asking whether a and b are the same object in memory.

Use any for More Complex Conditions

Sometimes what you really want is not "is this exact value in the list" but "does any element satisfy a rule." In that case, use any.

python
1users = [
2    {"name": "Alice", "active": False},
3    {"name": "Bob", "active": True},
4]
5
6has_active_user = any(user["active"] for user in users)
7print(has_active_user)

This is often the clean replacement for people searching for a custom contains function. in is for direct membership. any is for rule-based membership.

Another example:

python
1emails = ["[email protected]", "[email protected]"]
2target = "[email protected]"
3
4match = any(email.lower() == target.lower() for email in emails)
5print(match)

That is more expressive than forcing everything through a fake contains API.

Switch to a Set for Frequent Lookups

If membership checks are the main operation, convert the collection to a set.

python
1blocked_users = {"alice", "bob", "carol"}
2
3candidate = "bob"
4
5if candidate in blocked_users:
6    print("blocked")

A set is optimized for membership testing, so repeated lookups are much faster on average than repeatedly scanning a list. This is a data-structure choice, not a syntax change. You still write in, but the underlying container is better suited to the workload.

Keep the Code Idiomatic

It is technically possible to write your own helper:

python
def contains(items, value):
    return value in items

But it usually adds nothing. Python already gives you the cleanest form directly in the language. A helper only makes sense when it adds domain meaning, such as case-insensitive matching or checking nested fields.

Common Pitfalls

  • Looking for my_list.contains(value). Python lists do not provide that method.
  • Using is when you mean equality. Membership testing is usually about ==, not object identity.
  • Forgetting that in on a dictionary checks keys, not values.
  • Using a list for heavy lookup workloads when a set would be a better container.
  • Using in on a list of complex objects when you really need a custom condition with any.

Summary

  • In Python, the short contains check for lists is value in my_list.
  • Use not in for the negative form.
  • Use any(...) when the condition is more complex than direct equality.
  • Prefer a set when you perform many membership checks.
  • Avoid inventing a contains helper unless it adds real domain-specific behavior.

Course illustration
Course illustration

All Rights Reserved.