You can determine the maxval
with max
:
maxval = max(my_list)
Then get the indices using enumerate
and a list comprehension:
indices = [index for index, val in enumerate(my_list) if val == maxval]
For your example, I get
maxval == 58
indices = [5, 10, 11]
As per Keyser’s suggestion, you could save iterating over the list twice (once to determine maxval
, once to find matching index
es) by doing:
maxval = None
for index, val in enumerate(my_list):
if maxval is None or val > maxval:
indices = [index]
maxval = val
elif val == maxval:
indices.append(index)