-1

I am doing a Python course and am stuck on this question:

"Write a function called length_of_longest_word that accepts one list variable called word_list as an argument and returns the length of the longest word in that list."

Below is what I have written, appreciate your feedback!

    def length_of_longest_word(word_list):
        max_length = 0
        for max_length in word_list:
            max_length = max(max_length, length_of_longest_word)
        return max_length 

gavin
  • 793
  • 1
  • 7
  • 19
  • 3
    Does this answer your question? [Length of longest word in a list](https://stackoverflow.com/questions/14637696/length-of-longest-word-in-a-list) – dspencer Apr 01 '20 at 04:07
  • "Appreciate your feedback" is not a Stack Overflow question. Please repeat the intro tour. – Prune Apr 01 '20 at 04:34

3 Answers3

2

You are close. You have to use the actual built-in len somewhere:

def length_of_longest_word(word_list):
    max_length = 0
    for word in word_list:
        max_length = max(max_length, len(word))
    return max_length

You can also use some short-cuts, e.g. apply max on a longer iterable like all the lengthes at once:

def length_of_longest_word(word_list):
    return max(map(len, word_list), default=0)

Some links to docs: len, max, map

user2390182
  • 72,016
  • 6
  • 67
  • 89
2

You can use max with key parameter.

max(map(len,word_list))

Which is equivalent to

max(len(word) for word in word_list)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
0

Have a look at this approach

def length_of_longest_word(word_list):
    max_length = 0
    for word in word_list:
        length = len(word)
        if length > max_length:
            max_length = length
    return length
XPhyro
  • 419
  • 1
  • 6
  • 16