Name

__contains__

Synopsis

__contains__(self,item)

The Boolean test y in x calls x .__contains__( y ). When x is a sequence, __contains__ should return True when y equals the value of an item in the sequence. When x is a mapping, __contains__ should return True when y equals the value of a key in the mapping. Otherwise, __contains__ should return False. If __contains__ is absent, Python performs y in x as follows, taking time proportional to len( x ):

for z in x:
    if y=  =z: return True
return False

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.