Is there a short contains function for lists?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Is there a shortcut to create padded array in JavaScript?
- Is there a simple way of parsing this text into a Map
- Is there a simple way to delete a list element by value?
- Is there a simple way to delete a list element by value?
- Is there a standardized method to swap two variables in Python?
- Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?
- Is there a tree structure or algorithm to shuffle around levels in a tree?
- Is there a true single-pair shortest path algorithm?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.