index of min element of sequence

Roger Miller roger.miller at nova-sol.com
Mon Jan 21 14:47:50 EST 2008


On Jan 21, 8:48 am, Peter Otten <__pete... at web.de> wrote:

> Neal Becker wrote:
> > What's a good/fast way to find the index of the minimum element of a
> > sequence?
...

> >>> min(xrange(len(items)), key=items.__getitem__)
...

Or just
   items.index(min(items))
I found this to be significantly faster in a simple test (searching a
list of 1000 ints with the minimum in the middle), despite the fact
that it requires two passes.  I'm sure that one could find cased where
Peter's approach is faster, so you if you are concerned about speed
you should measure with your own data.






More information about the Python-list mailing list