0
how to get INDEX of each values in LIST which are SAME
let a list, a=[20,40,50,20,80] then p=[a.index(x) for x in a if x==20] output of p will be [0,0] but i wanted the output as [0, 3] why it is so
2 Respuestas
+ 6
Hi Rakesh!
Hope you know that index() will give the first occurrence of an item in a list.
print(a.index(20))
Inorder to handle this problem, you can use enumerate() method.
So, your code needs to be like this
a=[20,40,50,20,80]
p=[x for x,y in enumerate(a) if y==20]
print(p)
output: [0,3]
+ 1
Thank you.. I got your answer..