We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

The in operator

The operators in and not in test for set membership. x in s evaluates to true if x is a member of the set s, and false otherwise. x not in s returns the negation of x in s.

The set membership test has traditionally been bound to sequences; an object is a member of a set if the set is a sequence and contains an element equal to that object. However, it is possible for an object to support membership tests without being a sequence. In particular, dictionaries support membership testing as a nicer way of spelling dict.has_key(key); other mapping types may follow suit.

For the list and tuple types, x in y is true if and only if there exists an index i such that x == y[i] is true.

For the Unicode and string types, x in y is true if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Note, x and y need not be the same type; consequently, u'ab' in 'abc' will return true. Empty strings are always considered to be a substring of any other string, so “” in “abc” will return true. Changed in version 2.3: Previously, x was required to be a string of length 1.

For user-defined classes which define the __contains__ method, x in y is true if and only if y.__contains__(x) is true.

For user-defined classes which do not define __contains__ and do define __getitem__, x in y is true if and only if there is a non-negative integer index i such that x == y[i], and all lower integer indices do not raise IndexError exception. (If any other exception is raised, it is as if in raised that exception).

The operator not in is defined to have the inverse true value of in.