feature request: string.contains('...')

Ethan Furman ethan at stoneleaf.us
Fri Sep 24 20:01:27 CEST 2010

John Posner wrote:
> Another "missing feature" candidate: sublist
>    >>> 'bc' in 'abcde'
>    True
>    >>> list('bc') in list('abcde')
>    False

I'm not aware of any idioms, but how about a simple function?

def listinlist(list1, list2):
     "checks if list1 is in list2"
     if not list1:
         return True
     if not list2:
         return False
     length = len(list1)
     pos = 0
     while True:
             pos = list2.index(list1[0], pos)
         except ValueError:
             return False
         if list2[pos:pos+length] == list1:
             return True
         pos += 1


More information about the Python-list mailing list