Implement a python function longestWord which take input as list of words and return the longest word


Topic: Implement a python function longestWord which take input as list of words and return the longest word

Solution

import functools
def longestWord(word_list):
    if word_list is None or isinstance(word_list, list) == False or len(word_list) == 0:
        raise ValueError("Input word_list to function longestWord must be list of words of size at least 1")
    
    if len(word_list) == 1:
        return word_list[0]    
    else:
        return functools.reduce(lambda x,y: x if len(x) >= len(y) else y, word_list)
		
		



List all Python Programs