Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Numbers and Binary Data

Python in and not in operators

Reference for Python's in and not in operators: test membership in lists, strings, dicts and sets, how __contains__ and iteration work, and speed differences.

The in operator tests whether a value is a member of a container and returns True or False. not in is its exact opposite. They work on strings (substring test), lists, tuples, sets, dictionaries (keys), ranges, bytes and any object that supports membership.

Behind the scenes Python asks the container: first through __contains__, then, if that is missing, by iterating and comparing items with ==, and finally through the old __getitem__ protocol.

Python
"py" in "python"        # True
3 in [1, 2, 3]          # True
"a" not in {"a": 1}     # False

Try it

Python Membership in common containers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Your own __contains__

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
value in container
value not in container
Part Description
value The item to look for. For strings it must itself be a string.
container Any object with __contains__, or any iterable.

Return value: a bool. The result of __contains__ is converted to bool.

Raises: TypeError if the right operand is neither a container nor iterable, if a string test uses a non-string on the left, or if a set or dict lookup needs a hash and the value is unhashable.

Usage notes

  • Strings test substrings. "ab" in "cabd" is true, and the empty string is in every string.
  • Dictionaries test keys. Use value in d.values() for values and (k, v) in d.items() for pairs.
  • Speed differs a lot. Sets, frozensets and dicts answer in roughly constant time (they hash). Lists, tuples and generators scan from the start, so checking membership in a big list inside a loop becomes quadratic. Convert to a set first when you test many times.
  • Comparison is == after an identity check. Each item is considered a match if item is value or item == value. That is why a list containing the same NaN object finds it, although nan == nan is false.
  • Unhashable values (lists, dicts, sets) cannot be searched for in sets or dict keys, but a list can contain them: [1] in [[1], [2]] is true.
  • Ranges are smart. n in range(...) does arithmetic instead of looping, so it is fast even for huge ranges.
  • Generators are consumed. Testing x in gen advances the generator up to the match and the items it passed are gone.
  • Precedence. in belongs to the comparison operators, so it chains: a in b in c means a in b and b in c, which is rarely what you want. Parenthesize if you mix it with ==.
  • Use not in, not not (x in y) and not not x in y; the first reads best and all are equivalent.