Finding the index of each element in list containing duplicated values [closed]


Hello, Let’s say I have a list called values that contains some dupliated elements [330, 210, 130, 60, 0, 240, 280, 1300, 740, 640, 520, 430, 240, 240, 240, 200, 200]. I want to get the index of each element even it’s the duplicated ones. Here is what I did

l = []
for item in values:
    if item < t_max:
        l.append(values.index(item))

but it gives the following result

0
1
2
3
4
5
6
8
9
10
11
5
5
5
15
15

Any idea how to preceed? Thank you.

Answer

Try enumerate instead:

for index, item in enumerate(values):
    if item < t_max:
        l.append(index)

Your code doesn’t work because index takes the first occurrence of a value in a list, but in your list they’re duplicate values.