How to find all positions of the maximum value in a list?


Question

I have a list:

a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50,
             35, 41, 49, 37, 19, 40, 41, 31]

max element is 55 (two elements on position 9 and 12)

I need to find on which position(s) the maximum value is situated. Please, help.

1
124
1/27/2016 3:33:15 PM

Accepted Answer

>>> m = max(a)
>>> [i for i, j in enumerate(a) if j == m]
[9, 12]
182
10/21/2010 3:17:24 PM

a.index(max(a))

will tell you the index of the first instance of the largest valued element of list a.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon