[Tutor] index of max value in a list
Kent Johnson
kent37 at tds.net
Mon Dec 25 13:58:36 CET 2006
anil maran wrote:
> the max function, it returns the maximum value in the
> list rather than the index associated with that value.
>
> How do I return the index?
l.index(max(l)) will give you the index of the first occurrance of the
maximum.
m = max(l)
[ i for i,v in enumerate(l) if v==m ]
will give you a list of all indices where the max occurs. (Putting the
'max(l)' outside the list comprehension prevents it from being evaluated
for each loop element.)
Kent
More information about the Tutor
mailing list