a06e
a06e

Reputation: 20774

Pythonic maxby function?

A maxby(list, f) takes a list of objects and a function f as arguments, such that f applied to any object in the list returns a number. Then maxby returns the object x in the list for which f(x) is maximum.

What's the pythonic way to write something like maxby(list, f)? Is it already implemented, or I have to write one?

Upvotes: 4

Views: 2407

Answers (1)

cuonglm
cuonglm

Reputation: 2806

It's builtin in Python, just use max() function with key parameter:

max(list, key=f)

Upvotes: 10

Related Questions