Computer Science
Write a short python code segment that prints the longest word in a list of words.
Python
Python List Manipulation
9 Likes
Answer
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
Answered By
4 Likes
Related Questions
Write a program that takes any two lists L and M of the same size and adds their elements together to form a new list N whose elements are sums of the corresponding elements in L and M. For instance, if L = [3, 1, 4] and M = [1, 5, 9], then N should equal [4,6,13].
Write a program rotates the elements of a list so that the element at the first index moves to the second index, the element in the second index moves to the third index, etc., and the element in the last index moves to the first index.
Write a program that creates a list of all the integers less than 100 that are multiples of 3 or 5.
Define two variables first and second so that first = "Jimmy" and second = "Johny". Write a short python code segment that swaps the values assigned to these two variables and prints the results.