Max function question: How do I return the index of the maximum value of a list?

Robert Kern robert.kern at gmail.com
Sun Jun 4 21:52:12 EDT 2006


Steven Bethard wrote:
> Can you do something like::
> 
>      max_val, max_index = max((x, i) for i, x in enumerate(my_list))
> 
> ?  If any two "x" values are equal, this will return the one with the 
> lower index.  Don't know if that matters to you.

Wouldn't it return the one with the highest index?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list