Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 561
becko Avatar asked Oct 25 '25 05:10

becko


1 Answers

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

max(list, key=f)
like image 98
cuonglm Avatar answered Oct 26 '25 20:10

cuonglm