python - If statement check list contains is returning true when it shouldn't -


i have list contains values:

['1', '3', '4', '4'] 

i have if statement check if values contained within list output statement:

if "1" , "2" , "3" in columns:     print "1, 2 , 3" 

considering list doesn't contain value "2", should not print statement, is:

output:

1, 2 , 3 

can explain why case? way python reads list making occur?

it gets evaluated in order of operator precedence:

if "1" , "2" , ("3" in columns): 

expands into:

if "1" , "2" , true: 

which evaluates ("1" , "2") leaving with:

if "2" , true 

finally:

if true: 

instead can check if set of strings subset of columns:

if {"1", "2", "3"}.issubset(columns):     print "1, 2 , 3" 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -