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

28 Jul, 2021
The problem is to go through all the words in an array and the program should return the word with the longest one. Consider for example we are having an array, and we have numbers in alphabetic form, now when we pass this array as an input then we should get the word with the longest one. Below I had explained it with an example in order to give an elaborate view. Example:
Input :  [“one”, “two”, “three”, “four”] Output:  threeExplanation: As we pass the above array as a input, our program will check which word or which value has the maximum length and after completely iterating through the array then it returns the word with longest length.  
Methods 1#: Iterating and finding the greater length word.
Approach:
First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.Now, take a list where you have all the values.We are going to take this list, and we will iterate through each item using for loop.Then we take two variables max1 and temp to store the maximum length and the word with the longest length.After completing the above steps then we take the first value in the list and the first value length in order to compare.Once the above steps are completed we compare the items in the list using for loop. Below I had mentioned the logic.After completing the above steps run the program, and then we’ll get the required result.
First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.
Now, take a list where you have all the values.
We are going to take this list, and we will iterate through each item using for loop.
Then we take two variables max1 and temp to store the maximum length and the word with the longest length.
After completing the above steps then we take the first value in the list and the first value length in order to compare.
Once the above steps are completed we compare the items in the list using for loop. Below I had mentioned the logic.
After completing the above steps run the program, and then we’ll get the required result.
Implementation:
Python3
# function to find the longest# length in the listdef longestLength(a):    max1 = len(a[0])    temp = a[0]     # for loop to traverse the list    for i in a:        if(len(i) > max1):             max1 = len(i)            temp = i     print("The word with the longest length is:", temp,          " and length is ", max1)  # Driver Programa = ["one", "two", "third", "four"]longestLength(a)
Output:
The word with the longest length is: third  and length is  5
Methods 2#: Using sort().
Approach:
First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.Now, take a list where you have all the values.We are going to take this list, and we will iterate through each item using for loop.Then we take an empty list with the name final List and will append all the items.After appending we will sort the list using the sort() method.Now, as the list is sorted we can get the longest length, and we can display it.After completing the above steps run the program, and then we’ll get the required result.
First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.
Now, take a list where you have all the values.
We are going to take this list, and we will iterate through each item using for loop.
Then we take an empty list with the name final List and will append all the items.
After appending we will sort the list using the sort() method.
Now, as the list is sorted we can get the longest length, and we can display it.
After completing the above steps run the program, and then we’ll get the required result.
Python3
# function to find the longest length in the listdef longestLength(words):    finalList = []         for word in words:        finalList.append((len(word), word))         finalList.sort()         print("The word with the longest length is:", finalList[-1][1],          " and length is ", len(finalList[-1][1]))  # Driver Programa = ["one", "two", "third", "four"]longestLength(a)
Output:
The word with the longest length is: third  and length is  5
sooda367
Python list-programs
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.