Primary
in ⚬ᵖʸ|Documentation|1st|20251021201952-00-⌔
6. Expressions — Python 3 documentation#in
6.10.2. Membership test operations
The operators
inandnot intest for membership.x in sevaluates toTrueif x is a member of s, andFalseotherwise.x not in sreturns the negation ofx in s. All built-in sequences and set types support this as well as dictionary, for whichintests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expressionx in yis equivalent toany(x is e or x == e for e in y).For the string and bytes types,
x in yisTrueif and only if x is a substring of y. An equivalent test isy.find(x)!= -1. Empty strings are always considered to be a substring of any other string, so"" in "abc"will returnTrue.For user-defined classes which define the
__contains__()method,x in yreturnsTrueify.__contains__(x)returns a true value, andFalseotherwise.For user-defined classes which do not define
__contains__()but do define__iter__(),x in yisTrueif some valuez, for which the expressionx is z or x == zis true, is produced while iterating overy. If an exception is raised during the iteration, it is as ifinraised that exception.Lastly, the old-style iteration protocol is tried: if a class defines
__getitem__(),x in yisTrueif and only if there is a non-negative integer index i such thatx is y[i] or x == y[i], and no lower integer index raises theIndexErrorexception. (If any other exception is raised, it is as ifinraised that exception).The operator
not inis defined to have the inverse truth value ofin.Printed 2026-06-28.
Link to original
Secondary
• • •