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

This is a Python Program to read a list of words and return the length of the longest one.

Problem Description

The program takes a list of words and returns the word with the longest length.

Problem Solution

1. Take the number of elements in the list and store it in a variable.
2. Accept the values into the list using a for loop and insert them into the list.
3. First assume that the first element is the word with the longest length.
4. Then using a for loop and if statement, compare the lengths of the words in the list with the first element.
5. Store the name of the word with the longest length in a temporary variable.
6. Display the word with the longest length
7. Exit.

Program/Source Code

Here is source code of the Python Program to read a list of words and return the length of the longest one. The program output is also shown below.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
    element=input("Enter element" + str(x+1) + ":")
    a.append(element)
max1=len(a[0])
temp=a[0]
for i in a:
    if(len(i)>max1):
       max1=len(i)
       temp=i
print("The word with the longest length is:")
print(temp)
Program Explanation

1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values of elements into the list.
3. The append function obtains each element from the user and adds the same to the end of the list as many times as the number of elements taken.
4. Assuming that the first element in the list has the longest length, its length is stored in a variable to be compared with other lengths later in the program.
5. Based on the above assumption, the first element is also copied to a temporary variable.
6. The for loop is used to traverse through the elements in the list.
7. The if statement then compares the lengths of other elements with the length of the first element in the list.
8. If the length of a particular word is the largest, that word is copied to the temporary variable.
9. The word with the longest length is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the number of elements in list:4
Enter element1:"Apple"
Enter element2:"Ball"
Enter element3:"Cat"
Enter element4:"Dinosaur"
The word with the longest length is:
Dinosaur
 
Case 2:
Enter the number of elements in list:3
Enter element1:"Bangalore"
Enter element2:"Mumbai"
Enter element3:"Delhi"
The word with the longest length is:
Bangalore

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.