Python 'in' operator

I am having a bit of trouble with the Python in operator. sltn contains 0, 1, and 2 as I checked in the previous line (with a print, said it contains [0, 1, 2]) but for some reason the if ("0","1","2") in sltn: isn't detecting it.


Here is my code:

print (sltn)

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

Sorry, but the answers don't seem to have solved my problem :/

Hey, for some reason I do not understand this doesn't complete the loop and mark it as true, I have tested for sltn containing 1, 2 and 3, and none of them are detected.

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


I had to make it from

("0", "1", "2")


[1, 2, 3]


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

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

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

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

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)

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)

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.

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")):

In case you don't want waste time and iterate through all the data in your list, as widely suggested around here, you can do as follows:

a = ['1', '2', '3']
b = ['4', '3', '5']

test = set(a) & set(b)
if test:
    print('Found it. Here it is: ', test)

Of course, you can do if set(a) & set(b). I didn't do that for demonstration purposes. Note that you shouldn't replace & with and. They are two substantially different operators.

The above code displays:

Found it. Here it is:  {'3'}

Need Your Help

UIStackView before iOS 9.0

ios xcode ios9

I recently upgraded XCode to the latest Version 7.0 beta 3 (7A152u).

Twitter Bootstrap 3.0 row wider than window

css twitter-bootstrap

I was fiddling around with Twitter Bootstrap and noticed that my .row was wider than the screen length.