Write a Python function that takes a list of words and return the longest word and the length of the longest one.
Question
Write a Python function that takes a list of words and return the longest word and the length of the longest one.
Solution
Sure, here is a Python function that does exactly that:
def find_longest_word(word_list):
longest_word = max(word_list, key=len)
return longest_word, len(longest_word)
# Test the function
words = ["Python", "Java", "C", "JavaScript", "C++"]
print(find_longest_word(words))
In this function, we use the built-in max
function with key=len
to find the longest word in the list. The max
function returns the item from the list with the maximum value according to the function specified by key
. In this case, len
is used, so max
returns the longest string. The length of the longest word is then found using the len
function.
Similar Questions
How do you get the length of a list in Python?Answer( Please choose a correct answer ) CLEARlen(my_list)my_list.size()my_list.count()length(my_list)
Which built-in function is used to find the length of a sequence (such as a string or list) in Python? count() length() len() size()
Write a program to count number of words in a given string whose length is greater than 3.For example:Input Resultpython is easier than C3
Longest Substring Without Repeating CharactersGiven a string s find the length of the longest substring without repeating characters.
Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.