- 1
Give me a shorter code for python.
#this is a code to find the longest word in a given text #this is a code i wrote #i want a shorter code to do the same task txt = input() txt2=txt.split(" ") list=[] for i in txt2: list.append(len(i)) mx=max(list) for n in txt2: if len(n)==mx: print(n)
2 Answers
+ 3
txt = input()
print(max(txt.split(" "),key=len))
like this?
+ 1
print(sorted('cherry kiwi apple banana watermelon strawberry peach'.split(), key=len)[-1])
What if there are multiple words with same length? Does it matter?