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.
Python Tuple count() The count() method returns the number of times the specified element appears in the tuple.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
The tuples are checked for being identical using the '==' operator. This is assigned to a value. It is displayed on the console.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With