Bolster
Bolster

Reputation: 7916

Returning the argument from a list comprehension that's result satisfies <something>

It's a horrible title, I know; the basic explanation of what I mean is:

y=min([func(x) for x in range(z)])

As is, y contains the value of the output of func(x_min). (x_min might not actually be the smaller value in the range).

Basically, I'm looking for a way to get x_min in this generic case in a more pythonic way. Any ideas?

UPDATE: From SilentGhosts answer, (RTFM...), the equivalent of the above would be;

x_min=min(range(z),key=func)

Upvotes: 0

Views: 88

Answers (1)

SilentGhost
SilentGhost

Reputation: 320009

Use key argument:

min(iterable, key=func)

Upvotes: 4

Related Questions