0

Longest word in a list in python

I cleared two test cases not sure about the 3 and 4 https://code.sololearn.com/cytB1eo0hahR/?ref=app

1st Feb 2021, 5:44 AM
THIRU KUMARAN T R
THIRU KUMARAN T R - avatar
2 Answers
+ 3
Your code will automatically print the second element in the list (eventhough it is not the longest string) because of the condition "if lst[0] != max" This will be always True on second iteration: To fix: -> set the first element of the lst as default "max". max = lst[0] -> change the max if it is longer than the previous one. -> print max outside loop or after iteration. If you need more explanation, please feel free to ask. Thanks! https://code.sololearn.com/cA8A15A6A4A1
1st Feb 2021, 5:51 AM
noteve
noteve - avatar
+ 1
Thanks bro now I understand : )
1st Feb 2021, 5:54 AM
THIRU KUMARAN T R
THIRU KUMARAN T R - avatar