reduce expression to test sublist

chaouche yacine yacinechaouche at
Sat Jan 5 20:59:56 CET 2013

Because reduce doesn't do what you want. You'd want "all".

L1 = [1,2,3]
L2 = ["A1","B2","C3",1,2,3]
print all((x in L2 for x in L1)) # prints True
L3 = ["A1","B2","C3"]
print all((x in L2 for x in L3)) # prints True

----- Original Message -----
From: Asim <asim.r.p at>
To: python-list at
Sent: Saturday, January 5, 2013 7:25 PM
Subject: reduce expression to test sublist

Hi All

The following reduce expression checks if every element of list lst1 is present in list lst2.  It works as expected for integer lists but for lists of strings, it always returns False.

   reduce( lambda x,y: (x in lst2) and (y in lst2), lst1)

Moreover, for the lists of strings the following for-loop gives correct results when the above reduce expression doesn't.

   isSublist = True
   for i in lst1:
      isSublist = isSublist and (i in lst2)
      if not isSublist:
         isSublist = False

Can someone help me understand why?


More information about the Python-list mailing list