min, max with position

Dan Stromberg drsalists at gmail.com
Sun Jun 5 00:23:34 EDT 2022


On Sat, Jun 4, 2022 at 9:07 PM Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:

> On 5/06/22 10:07 am, dn wrote:
> > On 05/06/2022 09.50, Chris Angelico wrote:
> >>>>> min(enumerate(l), key=lambda x: x[1])
> >> (0, 1.618033)
> >
> > But, but, but which of the above characters is an 'el' and which a
> 'one'???
> > (please have pity on us old f...s and the visually-challenged!)
> >
>
> ell = l
> one = 1
> min(enumerate(ell), key=lambda x: x[one])
>
> Hope that clears it up!!!!!11!one!ell
>

I'm kind of partial to:
min((value, index) for (index, value) in enumerate(list_))

It'll return a single 2-tuple where the value of interest is at position
0.  In the event of a tie, it should give you the first such value
encountered.


More information about the Python-list mailing list