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:
Output:
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:
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.
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:
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.
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:
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.
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:
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
iswhen you mean equality. Membership testing is usually about==, not object identity. - Forgetting that
inon a dictionary checks keys, not values. - Using a list for heavy lookup workloads when a set would be a better container.
- Using
inon a list of complex objects when you really need a custom condition withany.
Summary
- In Python, the short contains check for lists is
value in my_list. - Use
not infor 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
containshelper unless it adds real domain-specific behavior.

