all Python 3operatorfunction
operator.contains
sequences
Membership test: b in a (note the argument order).
Syntax
operator.contains(a, b)
The argument order is the reverse of b in a: container first, then the item. That makes partial(operator.contains, allowed) a ready predicate "x is in allowed" for filter. For a dict it checks keys, not values: contains({'a': 1}, 1) is false. Speed depends on the container: set and dict answer almost instantly, a list is scanned.
Examples
import operator
print(operator.contains([1, 2, 3], 2)) # → True
print(operator.contains('python', 'th')) # → True
print(operator.contains({'a': 1}, 'a')) # → True
print(operator.contains({'a': 1}, 1)) # → False
print(operator.contains(2, [1, 2, 3])) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy