How do I create a function sublist()
that takes two lists, list1
and list2
, and returns True
if list1
is a s
How about this: let's approach this from the other side:
def sublist(a,b):
"""returns True if a is contained in b and in the same order"""
return a == [ch for ch in b if ch in a]
This will fail in some circumstances (e.g. should [1,2,3]
be a subset of [1,1,8,2,3]
) but it's hard to say exactly how you want this implemented...