[1, 2] in [1, 2, 3] returns 0?
ignacio at openservices.net
Sat Aug 25 20:46:39 CEST 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
> Type "copyright", "credits" or "license" for more information.
> >>> lst = [1,2,3]
> >>> sublst = [1,2]
> >>> sublst in lst
> 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
> >>> def issublist(sublst, lst):
> for item in sublst:
> if item not in lst: return 0
> return 1
> >>> issublist([1,2], [1,2,3])
> >>> issublist([1,4], [1,2,3])
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