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

← All Python list methods · Dict methods · list vs tuple vs set