Python Glossary Русский

all Python 3Conditionalsconstruct

in

membership

The membership operator. It checks whether an item is contained in a collection. Works with strings, lists, tuples, sets and dictionaries (by key).

Syntax

элемент in коллекция -> bool

The hidden cost: on a list, tuple or string, in is a linear O(n) scan, whereas on a set or dict it's a near-instant O(1) hash lookup. If you keep testing membership against a large collection, store it as a set, not a list.

Examples

print("a" in "banana")  # → True
print(3 in [1, 2, 3, 4])  # → True
print(5 not in [1, 2, 3])  # → True
d = {"a": 1, "b": 2}
print("a" in d)  # → True (по ключам)
print(1 in d.values())  # → True (по значениям)
print("py" in "python")  # → True (подстрока)
print(7 in {1, 3, 7, 9})  # → True (множество)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy