I want to write a function that determines if a sublist exists in a larger list.
list1 = [1,0,1,1,1,0,0]
list2 = [1,0,1,0,1,0,1]
#Should return true
sublistExists(list1, [1,1,1])
#Should return false
sublistExists(list2, [1,1,1])
Is there a Python function that can do this?
Might as well throw in a recursive version of @NasBanov's solution
No function that I know of
As Mark noted, this is not the most efficient search (it's O(n*m)). This problem can be approached in much the same way as string searching.
Simply create sets from the two lists and use the issubset function:
Here is a way that will work for simple lists that is slightly less fragile than Mark's