Length of longest word in a list

后端 未结 6 1236
面向向阳花
面向向阳花 2020-12-06 19:10

What is the more pythonic way of getting the length of the longest word:

len(max(words, key=len))

Or:

max(len(w) for w in words)

6条回答
  •  眼角桃花
    2020-12-06 19:40

    I'd say

    len(max(x, key=len))
    

    looks quite good because you utilize a keyword argument (key) of a built-in (max) with a built-in (len). So basically max(x, key=len) gets you almost the answer. But none of your code variants look particularly un-pythonic to me.

提交回复
热议问题