q = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5] vm = [-1, -1, -1, -1,1,2,3,1] p = [] for v in vm: if v in q: p.append(q.index(v)) else: p.append(99999) print p p = [q.index(v) if v in q else 99999 for v in vm] print p
Output:
[99999, 99999, 99999, 99999, 0, 1, 2, 0] [99999, 99999, 99999, 99999, 0, 1, 2, 0]
Instead of using append() in understanding the list, you can refer to p as direct output and use q.index(v) and 99999 in LC.
Not sure if this is intentional, but note that q.index(v) will only find the first occurrence of v , even if you have several in q . If you want to get the index of all v in q , consider using an enumerator and a list of indexes already visited
Something in these lines (pseudo-code):
visited = [] for i, v in enumerator(vm): if i not in visited: p.append(q.index(v)) else: p.append(q.index(v,max(visited)))