How to check if one of the following items is in a list?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
A common task in Python is checking whether any item from a group of candidates exists in a list. For example, you might want to know if a user has any of several required permissions, or if a shopping cart contains any items from a promotional category. This article covers multiple approaches, from the most Pythonic to performance-optimized techniques for large datasets.
The Simple Case: Checking for a Single Item
Before checking for multiple items, here is how you check for a single item using Python's in operator:
The in operator performs a linear scan through the list. For a single lookup, this is clean and readable.
Checking if Any of Several Items Is in a List
Method 1: Using any() with a Generator Expression
The most Pythonic way to check if at least one item from a group exists in a list is to use any():
any() short-circuits, meaning it stops as soon as it finds the first True value. If "banana" is the second item checked, it returns True immediately without checking "kiwi".
Method 2: Using Set Intersection
Converting one or both collections to sets gives you access to set intersection, which is faster for larger datasets:
The & operator returns a set of common elements. Since a non-empty set is truthy in Python, this works directly in an if statement. You can also use the .intersection() method:
Method 3: Using not set.isdisjoint()
The isdisjoint() method returns True if two sets have no elements in common. Negating it tells you whether there is any overlap:
This approach is efficient because isdisjoint() also short-circuits internally. It stops checking as soon as it finds a common element.
Finding Which Items Matched
Sometimes you need to know not just whether a match exists, but which items matched:
For better performance with large lists, convert fruits to a set first:
Set membership checks run in O(1) average time compared to O(n) for list membership, making this significantly faster when fruits is large.
Performance Comparison
The choice of method matters when you are working with large collections. Here is how the approaches compare:
For small lists (under a few dozen items), all methods are effectively instant and you should pick whichever reads most clearly. For lists with thousands of items, converting to a set first and using isdisjoint() or set intersection gives the best performance.
Checking Across Different Data Types
These techniques work with any hashable types, not just strings:
Common Pitfalls
- Using
orincorrectly: Writingif "a" or "b" in my_listdoes not check for both items. It evaluates asif ("a") or ("b" in my_list)because"a"is always truthy. You must writeif "a" in my_list or "b" in my_list. - Forgetting that sets require hashable elements: Lists, dicts, and other mutable types cannot be added to sets. If your items are unhashable, stick with
any()and list-based checks. - Repeated set conversion: If you check membership multiple times against the same list, convert it to a set once and reuse the set rather than converting on every check.
- Case sensitivity with strings:
"Apple"and"apple"are different items. Normalize case before checking if case-insensitive matching is needed.
Summary
For checking if any of several items exists in a list, use any(item in my_list for item in targets) for readability or convert to sets and use intersection or isdisjoint() for performance. For small lists, any method works. For large datasets, sets provide O(1) lookups that make a meaningful difference. Always be careful with the or operator, as it does not distribute across in checks the way natural language suggests.

