6
L=[0,8,5,6,4,5,6,14,8]

I want to be able to return the index of [4,5,6] (returning 4 in this example).

MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56
  • 4
    Why are people voting to close? This is a perfectly legitimate question. – MyNameIsKhan Dec 23 '12 at 03:26
  • I think the not a real question votes are from people who believe you have 0rovided no example code of yours to compare against. Further a reply from you to someones answer clearly statss your question whereas this shows little to no effort. – Woot4Moo Dec 23 '12 at 03:33
  • duplicate of: http://stackoverflow.com/questions/7627548/find-sub-list-inside-a-list-in-python – Chronial Dec 23 '12 at 03:59

3 Answers3

2

Here's one way to implement it:

def find_in_list(l, x):
    for i in range(len(l) - len(x) + 1):
        if l[i:i+len(x)] == x:
            return i
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • This is what I'm currently doing; is there a built-in Python function that would make this a short one-liner at all? – MyNameIsKhan Dec 23 '12 at 03:26
1

Use this instead:

' '.join(map(str,L)).index(' '.join(map(str,x)))/2

EDIT: Thanks to isbadawi for noticing the silly mistake, and solilo for pointing out the huge mistake haha

jackcogdill
  • 4,900
  • 3
  • 30
  • 48
1
In [193]: min(i for i in range(len(L)) if L[i:i+len(key)] == key)
Out[193]: 4

In [194]: L=[0,8,5,6,4,5,6,14,8]

In [195]: key=[4,5,6]

In [196]: min(i for i in range(len(L)) if L[i:i+len(key)] == key)
Out[196]: 4
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241