Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'a' in ('abc') True while 'a' in ['abc'] is False?

Tags:

python

When using the interpreter, the expression 'a' in ('abc') returns True, while 'a' in ['abc'] returns False. Can somebody explain this behaviour?

like image 323
Alver Avatar asked Aug 09 '15 04:08

Alver


1 Answers

('abc') is the same as 'abc'. 'abc' contains the substring 'a', hence 'a' in 'abc' == True.

If you want the tuple instead, you need to write ('abc', ).

['abc'] is a list (containing a single element, the string 'abc'). 'a' is not a member of this list, so 'a' in ['abc'] == False

like image 159
dhke Avatar answered Sep 22 '22 03:09

dhke