To check whether an item exists in a Python list, use the in operator: item in my_list returns True or False. It is the most readable and idiomatic option, and it works for any element that supports ==.
A list lookup scans elements one by one, so it takes O(n) time. For a handful of items that is fine, but if you check membership often on a large collection, convert it to a set first.
The in operator compares the item against each element until it finds a match. It stops at the first match, so it is faster than methods that scan the whole list.
Use not in for the negative check instead of not (x in y). Both work, but not in reads more naturally and is the style recommended by PEP 8.
list.count(x) > 0 also gives the right answer, but it always walks the entire list, even after finding a match. list.index(x) raises ValueError when the item is missing, so you need a try block.
Use index() when you need the position, and in when you only need a yes or no answer.
When you need to test a property instead of an exact value, pass a generator expression to any(). It short-circuits on the first match and does not build a temporary list.
Use all() for the opposite question, whether every element satisfies a condition. Avoid any([... for ...]) with square brackets, because that builds the full list first.
If you want the matching element and not just a boolean, combine a generator expression with next() and a default value.
The default (None) is returned when nothing matches, so no exception is raised.
Membership in a set is O(1) on average, because it uses hashing. If you test many items against the same data, build the set once.
Set elements must be hashable, so lists and dicts cannot go in a set. For a sorted list, the bisect module gives O(log n) lookups without converting.
| Method | Time | Use when |
|---|---|---|
x in list | O(n) | Small lists or a one-off check |
x in set | O(1) average | Repeated checks on hashable items |
bisect on a sorted list | O(log n) | List is already sorted and must stay a list |
any(...) | O(n) | Checking a condition, not an exact value |
The in operator checks identity first and then equality, so it uses == of the elements. That has a few consequences worth knowing.
1 in [1.0] is True, because 1 == 1.0.True in [1] is True, because True == 1.float("nan") in [float("nan")] is False for two different NaN objects, because nan != nan. The check returns True only if it is the same object.__eq__ so that in behaves as you expect.For a single check, x in my_list is the fastest and simplest. For many checks, convert the list to a set once and test against the set.
Normalise both sides, for example "bob" in (name.lower() for name in names). For repeated checks, build {name.lower() for name in names} once.
Use not set(a).isdisjoint(b), or any(x in b_set for x in a) with b_set = set(b).
Yes. On a tuple it behaves like a list. On a string it tests for a substring, and on a dictionary it tests the keys, in O(1) average time.
Use item in my_list for a simple membership test and not in for the negative. Reach for any() when you test a condition, next() when you need the matching element, and a set or bisect when lookups are frequent or the list is large.