Skip to main content
edited tags; edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Python3: find Finding sub-list

Source Link
Dacav
  • 257
  • 2
  • 6

Python3: find sub-list

Pythonic way of expressing the simple problem:

Tell if the list needle is sublist of haystack


#!/usr/bin/env python3

def sublist (haystack, needle):

    def start ():
        i = iter(needle)
        return next(i), i

    try:
        n0, i = start()
        for h in haystack:
            if h == n0:
                n0 = next(i)
            else:
                n0, i = start()
    except StopIteration:
        return True
    return False