inspection
x in list
Membership test operator that returns True if any element equals x, else False. Runs in O(n) — for repeated lookups, convert to a set (O(1) per test).
x in list | x not in list Parameters
| Parameter | Purpose |
|---|---|
| in | True when at least one element == x. |
| not in | Inverse form; equivalent to `not (x in list)`. |
Examples
>>> 2 in [1, 2, 3]
True Basic membership check.
>>> 'a' in ['A', 'B']
False Uses ==; string comparison is case-sensitive.
>>> big = list(range(10_000))
>>> lookup = set(big)
>>> 9999 in lookup # O(1) vs O(n) for the list
True For hot lookups, promote to a set.
>>> 1 in [True, 2]
True True == 1 — booleans and ints collide under ==.
Gotcha
O(n) per test — a set/dict is O(1). Comparison uses == so True/1 and False/0 are treated as equal.
Related methods
list.count
Returns the number of times x appears in the list. Comparison uses equality; returns 0 if x is not present (never raises).
list.index
Returns the zero-based index of the first item equal to x within the optional [start, end) range. Raises ValueError if no match is found.
len (built-in)
Returns the number of elements in the list as an int. O(1) because CPython lists track their length internally.
← All Python list methods · Dict methods · list vs tuple vs set