Answer the question
In order to leave comments, you need to log in
How to sort a list using lambda?
Good day!
There is a list:
lst = ['1:ooo', '1:ii', '1:uuu', '2:dd','=:nnn' , '=:gg', '2:ee','2:sss' , '2:aa']
["1:ooo", "1:uuu", "2:sss", "=:nnn", "1:ii", "2:aa", "2:dd", "2:ee", "=:gg"]
['1:ii', '2:aa', '2:dd', '2:ee', '=:gg', '1:ooo', '1:uuu', '2:sss', '=:nnn']
lst.sort(key=lambda x:(len(x), x[0], x[2]), reverse=True)
['=:nnn', '2:sss', '1:uuu', '1:ooo', '=:gg', '2:ee', '2:dd', '2:aa', '1:ii']
Answer the question
In order to leave comments, you need to log in
Put a minus in front of len, it will be in reverse order in length
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question