Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python 'in' operator to check my list/tuple contains each of the integers 0, 1, 2?

How do I use the Python in operator to check my list/tuple sltn contains each of the integers 0, 1, and 2?

I tried the following, why are they both wrong:

# Approach 1
if ("0","1","2") in sltn:
     kwd1 = True

# Approach 2
if any(item in sltn for item in ("0", "1", "2")):
     kwd1 = True

Update: why did I have to convert ("0", "1", "2") into either the tuple (1, 2, 3)? or the list [1, 2, 3]?

like image 500
G.Armstrong Avatar asked Feb 09 '16 21:02

G.Armstrong


People also ask

How do you check if a list contains an integer in Python?

Use the all() function to check if all elements in a list are integers, e.g. if all(isinstance(item, int) for item in my_list): . The all() function will return True if all of the elements in the list are integers and False otherwise.

How do you check that tuple A contains all elements of tuple B?

To check if all items of a Tuple are True in Python, call all() builtin function and pass the tuple object as argument to it. all() returns True if all the items of Tuple are True, or else it returns False.


3 Answers

Using the in keyword is a shorthand for calling an object's __contains__ method.

>>> a = [1, 2, 3]
>>> 2 in a
True
>>> a.__contains__(2)
True

Thus, ("0","1","2") in [0, 1, 2] asks whether the tuple ("0", "1", "2") is contained in the list [0, 1, 2]. The answer to this question if False. To be True, you would have to have a list like this:

>>> a = [1, 2, 3, ("0","1","2")]
>>> ("0","1","2") in a
True

Please also note that the elements of your tuple are strings. You probably want to check whether any or all of the elements in your tuple - after converting these elements to integers - are contained in your list.

To check whether all elements of the tuple (as integers) are contained in the list, use

>>> sltn = [1, 2, 3]
>>> t = ("0", "2", "3")
>>> set(map(int, t)).issubset(sltn)
False

To check whether any element of the tuple (as integer) is contained in the list, you can use

>>> sltn_set = set(sltn)
>>> any(int(x) in sltn_set for x in t)
True

and make use of the lazy evaluation any performs.

Of course, if your tuple contains strings for no particular reason, just use
(1, 2, 3) and omit the conversion to int.

like image 102
timgeb Avatar answered Sep 28 '22 10:09

timgeb


if ("0","1","2") in sltn

You are trying to check whether the sltn list contains the tuple ("0","1","2"), which it does not. (It contains 3 integers)

But you can get it done using #all() :

sltn = [1, 2, 3] # list
tab = ("1", "2", "3") # tuple

print(all(int(el) in sltn for el in tab)) # True
like image 42
Mohammed Aouf Zouag Avatar answered Sep 28 '22 10:09

Mohammed Aouf Zouag


To check whether your sequence contains all of the elements you want to check, you can use a generator comprehension in a call to all:

if all(item in sltn for item in ("0", "1", "2")):
    ...

If you're fine with either of them being inside the list, you can use any instead:

if any(item in sltn for item in ("0", "1", "2")):
    ...
like image 34
L3viathan Avatar answered Sep 28 '22 12:09

L3viathan