14

I have seen an index function but it says it errors out if it can't find it. Is there a simple way to just check if the item exists? I just want to get a boolean value of the result so like:

if tuple.exists("item"):
    print "it exists"
Joe Wicentowski
  • 5,159
  • 16
  • 26
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

1 Answers1

34

Use in operator:

>>> 2 in (2, 3, 4)
True

in operator can be used to check for the membership of any element in a sequence.

And don't name your tuple as tuple. Use a different name.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525