- 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)

4th Jun 2022, 5:19 PM
Suprio Paul
Suprio Paul - avatar
2 ответов
+ 3
txt = input() print(max(txt.split(" "),key=len)) like this?
4th Jun 2022, 6:43 PM
Jihad AL AKL
Jihad AL AKL - avatar
+ 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?
4th Jun 2022, 6:37 PM
Lisa
Lisa - avatar