Index of maximum element in list

Terry Reedy tjreedy at udel.edu
Sat Jan 26 02:32:48 EST 2008


"Henry Baxter" <henry.baxter at gmail.com> wrote in message 
news:8d04436f0801251519g482a697dn625223b6d46e8f2c at mail.gmail.com...
| Thanks Hexamorph and Neal. Somehow I didn't make the connection with 
using
| 'index', but I'm all sorted out now :)
|
| On Jan 25, 2008 1:47 PM, Hexamorph <hexamorph at gmx.net> wrote:
|
| > Henry Baxter wrote:
| > > Oops, gmail has keyboard shortcuts apparently, to continue:
| > >
| > > def maxi(l):
| > >     m = max(l)
| > >     for i, v in enumerate(l):
| > >         if m == v:
| > >             return i
| > >
| >
| > What's about l.index(max(l)) ?

Both of these methods scan the list twice.  The two given by RH and SDD do 
so just once.  Both of those will give the index of the of the last maximum 
value.  If you want the index of the first max value (you did not specify 
;-), write an explicit loop. 






More information about the Python-list mailing list