0

I have a list in python that looks like below

a=[['David','McConor','123000','900']
['Timothy','Gerrard','123001','901']]

I desire to sort the list above using the last entry of each sublist as the key. I have succesfully sorted the list lexicographically using the a.sort() function. Now i want to sort the list and print each sublist such that the list with '901' comes first is my problem.

What I tried

##defining what mykey is where am stuck
mykey=
##the sort function has overloads for key
a.sort(key=mykey,reverse=True)

Thanks for your contribution.

2 Answers 2

0

You can use an alternative way

sorted(a, key = lambda x: (x[-1]), reverse=True)
Sign up to request clarification or add additional context in comments.

Comments

0

key expects a function, you can create a simple lambda function to check for the last item:

a.sort(key=lambda x: x[-1],reverse=True)

3 Comments

OKay trying it right away
this is ingenius, thanks
You're welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.