pythonlistindexingmax

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


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]

The maximum value is 55, which appears twice in the data.

How can I get all indices where the maximum value appears?


Solution

  • >>> m = max(a)
    >>> [i for i, j in enumerate(a) if j == m]
    [9, 12]