0

How do i return the longest word from a list

E.g. "This is a fine statement" I want to output to be "statement" which is the longest word after in the list after splitting it

17th Jul 2021, 12:34 PM
Philippe Ezekiel
Philippe Ezekiel - avatar
12 Respuestas
+ 2
Now here's the manual way: li = [...] # Note: This masks the max() function max = [""] for x in li: if len(x) > len(max[0]): max = [x] elif len(x) == len(max[0]): max.append(x) print(max) # Hope this helps
17th Jul 2021, 8:23 PM
Calvin Thomas
Calvin Thomas - avatar
+ 9
print(max(input().split(), key=len))
17th Jul 2021, 3:44 PM
David Ashton
David Ashton - avatar
+ 3
s = "google is cool until avbfgrthgjwtfkd" d = {len(i):i for i in s.split()} print(d[max(d)])
18th Jul 2021, 7:13 AM
Shadoff
Shadoff - avatar
+ 2
Any attempt from your side would be great
17th Jul 2021, 12:35 PM
Shadoff
Shadoff - avatar
+ 2
if words in the string do not repeat and length of words are not the same using dictionary is better
17th Jul 2021, 3:18 PM
Shadoff
Shadoff - avatar
+ 2
Azad m. A. Hi! When you write: max(input().split(' ')) you compare the the value of the strings, not the lenght of the words. This means for example that ”q” > ”aaa”. So with ”aaa q” as input your code gives 1 as a result.
17th Jul 2021, 10:20 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Shadoff Your code won't work if there are multiple words having the maximum length.
18th Jul 2021, 9:49 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Find all words in the list with the most number of letters: https://code.sololearn.com/cAMR8D1Qq0iX/?ref=app
17th Jul 2021, 7:35 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Calvin Thomas; probably the one starts with the highest order...anyway, that was a good point for them to expand the code, via including further conditions. Simply, throwing away any duplications found, there are several ways to do that:- such as turning the list to a SET and employing the higher orders competancing.......etc.
18th Jul 2021, 1:15 PM
Azad m. A.
Azad m. A. - avatar
+ 1
Here’s a way to use a dictionary in this context. The keys are the word length and the value is a list where all the words with that word leghth becomes elements in the list: https://code.sololearn.com/c49r3osFbDQn/?ref=app
18th Jul 2021, 3:15 PM
Per Bratthammar
Per Bratthammar - avatar
0
shadoff; your last print, how it works? I know that it works, but how d was used twice?
18th Jul 2021, 9:25 AM
Azad m. A.
Azad m. A. - avatar
- 1
or print(len(max(input().split(' ')))) ?
17th Jul 2021, 6:01 PM
Azad m. A.
Azad m. A. - avatar