Index of maximum element in list

Neal Becker ndbecker2 at gmail.com
Fri Jan 25 22:47:22 CET 2008


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
> 
> But it seems like something that should be built in - or at least I should
> be able to write a lambda function for it, but I'm not sure how to do that
> either...Suggestions are very much welcome!
> 

I really think this is a good candidate for a builtin.  I suggest:

max2 (x):
  """ return (minvalue,minindex)"""

This allows efficient usage in all cases, including iterators (not just
sequences).






More information about the Python-list mailing list