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

Ignacio Vazquez-Abrams ignacio at openservices.net
Sat Aug 25 14:46:39 EDT 2001


On 25 Aug 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.
> I guess a function such as issublist(sublst, lst) would be more
> appropriate:
>
> >>> def issublist(sublst, lst):
> 	for item in sublst:
> 		if item not in lst: return 0
> 	return 1
>
> >>> issublist([1,2], [1,2,3])
> 1
> >>> issublist([1,4], [1,2,3])
> 0
>
>
> regards,
>
>
> Sandy

from the Python Language Reference:

"For the list and tuple types, x in y is true if and only if there exists an
index i such that x == y[i] is true."

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list