[1, 2] in [1, 2, 3] returns 0?

Erik Max Francis max at alcyone.com
Sat Aug 25 20:33:24 CEST 2001


Sandy Norton wrote:

> Please excuse me if this is a dumb comment that has been made before.
> 
> Recently, I was rather surprised by the following behavior:
> 
> Python 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on
> win32
> Type "copyright", "credits" or "license" for more information.
> >>> lst = [1,2,3]
> >>> sublst = [1,2]
> >>> sublst in lst
> 0
> 
> I am sure there's a good reason why it would be considered unpythonic
> for the above to return 1... I just can't get my puny brain to figure
> it out right now.

The in operator, when used on a sequence returns true if the left-hand
operand is an _element_ of the sequence, regardless of whether that
operand is itself a sequence:

max at oxygen:~crank% python
Python 2.0 (#2, Apr  4 2001, 19:28:30) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> [1, 2] in [1, 2, 3]
0
>>> 1 in [1, 2, 3]
1
>>> [1, 2] in [[1, 2], [2, 3], [3, 4]]
1

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Do you like what you see / Do you like yourself
\__/ Neneh Cherry
    REALpolitik / http://www.realpolitik.com/
 Get your own customized newsfeed online in realtime ... for free!



More information about the Python-list mailing list