Open In App

Python Program to Return the Length of the Longest Word from the List of Words

Last Updated : 17 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with lists of words in Python, we may need to determine the length of the longest word in the list.

For example, given a list like [“Python”, “with”, “GFG], we would want to find that “Python” has the longest length. Let’s go through some methods to achieve this.

Using max()

max() function is one of the simplest and most efficient ways to find the longest word in a list by specifying a key to compare based on word length.

Python
a = ["geeks", "for", "geeks", "programming"]

# Use max() with key=len to find the word with the maximum length
res = len(max(a, key=len))
print(res)  

Output
11

Explanation:

  • We use the max() function, where the key argument is set to len(), allowing the comparison of word lengths.
  • max() returns the word with the greatest length and len() gives the length of that word.
  • This method is concise and efficient for finding the longest word in a list.

Let’s explore some more methods and see how we can return the length of the longest word from the list of words.

Using list comprehension

This method uses list comprehension to first create a list of word lengths and then apply the max() function to find the longest length.

Python
a = ["geeks", "for", "geeks", "programming"]

# Using list comprehension to create a list of word lengths and find the max length
res = max([len(w) for w in a])
print(res)  

Output
11

Explanation:

  • A list comprehension is used to generate a list of word lengths.
  • We then use max() to find the longest length from this list of lengths.

Using reduce() from functools

Using reduce() allows us to apply a function that iteratively compares and returns the longest length from the list of word lengths.

Python
from functools import reduce

a = ["geeks", "for", "geeks", "programming"]

# Use reduce() to iteratively find the maximum length
res = reduce(lambda a, b: a if a > b else b, [len(w) for w in a])
print(res)  

Output
11

Explanation:

  • We import the reduce() function from the functools module to apply a lambda function.
  • lambda function compares two values and returns the larger one, which is used iteratively over the list of word lengths.

Using sorted()

We can also use sorted() to arrange the words based on their lengths in descending order, then extract the length of the first word.

Python
a = ["geeks", "for", "geeks", "programming"]

# Use sorted() to sort words by length in descending order
res = len(sorted(a, key=len, reverse=True)[0])
print(res)  

Output
11

Explanation:

  • sorted() sorts the words in descending order based on their lengths.
  • We then select the first word in the sorted list (which is the longest) and find its length using len().

Using for loop

Another simple approach is to loop through the list and check the length of each word while keeping track of the longest one encountered.

Python
a = ["geeks", "for", "geeks", "programming"]

# Initializing a variable to store the longest word's length
max_len = 0

# Looping through each word in the list
for w in a:
    # Checking if the current word's length is greater than max_len
    if len(w) > max_len:
        max_len = len(w)

print(max_len)  

Output
11

Explanation:

  • We initialize the variable max_len to 0, which will hold the longest word’s length.
  • A for loop is used to iterate through the list of words.
  • Inside the loop, we compare each word’s length with the current value of max_len and update it if a longer word is found.


Next Article

Similar Reads