6

How do I check if a tuple contains values of 100 or 200 ?

I've tried:

long_c_ABANDONEDBABY = 100
long_c_HARAMI = 200
# also tried: if (100 or 200)
if (100, 200) in (long_c_ABANDONEDBABY, long_c_HARAMI):
    print "True"

But I get false positives, how can I do this?

The question Can Python test the membership of multiple values in a list? is about checking whether a tuple contains all of the given values, this question is about containing at least one of them.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268

1 Answers1

8

You may use any()function to make the checks like this as:

>>> my_tuple = (1, 2, 3, 4, 5, 6)
>>> check_list = [2, 10]

>>> any(t in my_tuple for t in check_list)
True

OR, explicitly make check for individual item using OR as:

>>> 2 in my_tuple or 10 in my_tuple
True
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • You could also use `len()` `>>> test = (1, )` `>>> test2 = (1, 2)` `>>> len(test)` `1` `>>> len(test2)` `2` So just a condition statement `if len(test) > 1:` – Rapido Dec 09 '20 at 14:17