3

Is there a magic method in python for the "in" method? For example,

y = [5, 4, 3, 2,1]
5 in y #returns true

if so what is it, and if not, how would one implement it.

1 Answers1

7

It's __contains__():

>>> y = [5, 4, 3, 2, 1]
>>> y.__contains__(3)
True
>>> y.__contains__(6)
False
arshajii
  • 127,459
  • 24
  • 238
  • 287