I would like to improve my understanding regarding the key
argument of the builtin max
function. Please consider the following example:
I have a list of lists, and I want to find the one with the biggest length. I can write the straight forward solution:
maximum = 0
for l in lists:
maximum = max(maximum, len(l))
However I would prefer to avoid an explicit for loop using the key
argument of max
. As I understand it, the key will apply its argument to each element in the input and then compare those outputs. So this should work:
maximum = max(lists, key=len)
I believe it should work because according to my (apparently false) understanding the above statement would be equivalent to applying length to each element and then invoking max
:
max([len(l) for l in text_tokenized])
What am I missing?