Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if tuple contains at least one of multiple values [duplicate]

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.

like image 227
Pedro Lobito Avatar asked Dec 16 '16 16:12

Pedro Lobito


People also ask

How do you count repeated elements in tuple Python?

Python Tuple count() The count() method returns the number of times the specified element appears in the tuple.

Can tuple have duplicates in Python?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

How do you check if all items in the tuple are the same?

The tuples are checked for being identical using the '==' operator. This is assigned to a value. It is displayed on the console.

How do you check if multiple items exist in a list Python?

Use the all() function to check if multiple values are in a list, e.g. if all(value in my_list for value in multiple_values): . The all() function will return True if all of the specified values are in the list and False otherwise.


1 Answers

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
like image 189
Moinuddin Quadri Avatar answered Sep 25 '22 22:09

Moinuddin Quadri