Sorting a list based on numeric value

Dear All

I have a list in this way

list = [ [a,b,2], [c,d,4] , [e,f,3] ]

How do I sort this list based on the 3rd element in the list ?

I want to get a new list which is like
[c,d,4] , [e,f,3], [a,b,2]

Thanks
Harish

Hi @hari.yajurveda,
Yes you can do it in various ways. One of them is using “key” argument in the sorted fuction :

sorted(list, key=lambda item: item[2], reverse=False)

You can change the reverse argument accordingly.

1 Like

Thank you very much. This solved my problem perfectly