1

I have an list of integers:

 x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 
      32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]

And second list that can only contain elements from x, for example: y = [44, 11, 49]

I need to find the index of each element of y in x.

cglacet
  • 8,873
  • 4
  • 45
  • 60
SilverCrow
  • 167
  • 1
  • 12

2 Answers2

1

.index() is what you're after.

x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]
y = [44, 11, 49]
for a in y:
    print(x.index(a))

EDIT: Provided that each element of y appears once, and only once in x.

JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
1
x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]
y = [44, 11, 49]
indexs=[x.index(i) for i in y if i in x]
print(indexs)

Or

ind=list(map(x.index,y))
print(ind)
MrNobody33
  • 6,413
  • 7
  • 19