I'm having difficulty in sorting a list of strings in a descending order in terms of the length of words. Please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I'm having difficulty in sorting a list of strings in a descending order in terms of the length of words. Please help

14th Jan 2019, 7:43 AM
Elvis Stephen
Elvis Stephen - avatar
6 Answers
+ 1
Consider the following example lst = ['r','ris','ri','rishi','rish'] #for ascending order sorted(lst,key=len) #for decending order sorted(lst,key=len)[::-1]
14th Jan 2019, 9:39 AM
Rishi Anand
Rishi Anand - avatar
+ 2
You can also do it by adding another argument to the call of sort or sorted: reverse=True
14th Jan 2019, 10:50 AM
HonFu
HonFu - avatar
+ 1
Please mention the programming language
14th Jan 2019, 9:28 AM
Rishi Anand
Rishi Anand - avatar
+ 1
Rishi Anand it worked. Thanks alot
14th Jan 2019, 9:48 AM
Elvis Stephen
Elvis Stephen - avatar
14th Jan 2019, 9:29 AM
Elvis Stephen
Elvis Stephen - avatar
0
If you can use this example to explain to me I'll appreciate. Countries=["Kenya", "Australia", "Canada", "USA"] I've been using countries.sort(key=len) to try and sort the list by length in a descending order
14th Jan 2019, 9:42 AM
Elvis Stephen
Elvis Stephen - avatar