Length of longest word in a list

后端 未结 6 1243
面向向阳花
面向向阳花 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条回答
  •  猫巷女王i
    2020-12-06 19:34

    Although:

    max(len(w) for w in words)
    

    does kind of "read" easier - you've got the overhead of a generator.

    While:

    len(max(words, key=len))
    

    can optimise away with the key using builtins and since len is normally a very efficient op for strings, is going to be faster...

提交回复
热议问题