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
12 Answers
+ 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
+ 9
print(max(input().split(), key=len))
+ 3
s = "google is cool until avbfgrthgjwtfkd"
d = {len(i):i for i in s.split()}
print(d[max(d)])
+ 2
Any attempt from your side would be great
+ 2
if words in the string do not repeat and length of words are not the same using dictionary is better
+ 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.
+ 2
Shadoff Your code won't work if there are multiple words having the maximum length.
+ 1
Find all words in the list with the most number of letters:
https://code.sololearn.com/cAMR8D1Qq0iX/?ref=app
+ 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.
+ 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
0
shadoff;
your last print, how it works?
I know that it works, but how d was used twice?
- 1
or
print(len(max(input().split(' '))))
?