CBSE Class 12 Computer Science Question 98 of 105

Python Revision Tour II — Question 5

Back to all questions
5
Question

Question 5

Write a short python code segment that prints the longest word in a list of words.

Solution
my_list = eval(input("Enter the list : "))
longest_word = ""  
max_length = 0  

for word in my_list:
    if len(word) > max_length:
        max_length = len(word)
        longest_word = word

print("Longest word:", longest_word)
Output
Enter the list : ['red', 'yellow', 'green', 'blue']
Longest word: yellow  
Enter the list : ['lion', 'elephant', 'tiger', 'monkey', 'hippopotamus']
Longest word: hippopotamus  
Answer