0

[solved] how to show both words if both words length are same ?

question :- print word from string which have maximum legnth a = "this is awesome feeling sufferi" list1 = a.split() d = {} for i in list1: b = len(i) d[b] = i print(d) max_i = max(d) print(d[max_i]) output :- sufferi in a string ...feeling and sufferi have same legnth ... but it prints only sufferi why?

28th Jan 2021, 4:16 PM
Hemant Kosrekar
Hemant Kosrekar - avatar
5 Respostas
+ 3
Hemant Kosrekar why it is printing only sufferi because at last you are assigning the value of key 7 as sufferi d={} d[7] = "awesome" d[7] = "feeling" d[7] = "sufferi" so last will be added to d that's why it's printing sufferi by the way you can print the max length word using max() function print(max(input().split(), key=len)) Edit : multi words of same length https://code.sololearn.com/c30tCLX9nRBq/?ref=app
28th Jan 2021, 4:33 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 2
a = "this is awesome feeling sufferi" a = a.split() B = [] Num = 0 For i in a: If len(i) >= num : Num = len(i) B.append(i) Print (*b,sep=' ')
28th Jan 2021, 4:39 PM
Shobhit
Shobhit - avatar
0
a = "this is awesome feeling sufferi" list1 = a.split() long = 0 for i in range (len(list1)): if len(list1[i]) > long: long = list1[i] print(long) Hemant Kosrekar i edited it, it works now
28th Jan 2021, 4:21 PM
Sadra Shakouri
Sadra Shakouri - avatar
0
Swift it shows type error : > operator not possible between str and int object nice logic but not works
28th Jan 2021, 4:30 PM
Hemant Kosrekar
Hemant Kosrekar - avatar
0
Hemant Kosrekar an edited version a = "this is awesome feeling sufferi" list1 = a.split() long = str(0) for i in range (len(list1)): if len(list1[i]) > len(long): long = list1[i] print(long)
28th Jan 2021, 4:34 PM
Sadra Shakouri
Sadra Shakouri - avatar