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.
("0","1","2")
isn't in[0, 1, 2]
... – Volkanif all(x in sltn for x in ("0","1","2")):
– Anarthria[0, 1, 2]
? Did you mean to type["0", "1", "2"]
. If slnt is[0, 1, 2]
, you need to changeif any(item in sltn for item in ("0", "1", "2")):
toif any(item in sltn for item in (0, 1, 2)):
since a list/tuple of ints is not the same as one of strings. – Hobbie(...)
with lists[...]
, but here that doesn't matter, list and tuple are both collections and supportin
operator. (But please don't call them arrays, those are different again). You're also confusingif x in y
withif y in x
andall(xx in y for xx in x)
and that does matter hugely. – Presentment