0

Is there any way in Python to get all indexes of elements which contain max value of a list?

I've tried this:

list = [1, 3, 2, 2, 1, 1, 1, 2, 3, 2, 1, 1, 1, 3, 1, 1, 3]
m = list.index (max(list))

But this returns only the first index of all of those which have max value.

MLapaj
  • 371
  • 3
  • 11

2 Answers2

4

Try this :

l = [1, 3, 2, 2, 1, 1, 1, 2, 3, 2, 1, 1, 1, 3, 1, 1, 3]
mx = max(l)
m = [i for i,j in enumerate(l) if j==mx]

OUTPUT :

[1, 8, 13, 16]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
2

You can compute the max then use a list comprehension to get the indices where the max is found:

l = [1, 3, 2, 2, 1, 1, 1, 2, 3, 2, 1, 1, 1, 3, 1, 1, 3]
x = max(l)

m = [i for i in range(len(l)) if l[i] == x]

print(m)

Output:

[1, 8, 13, 16]
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55