Sort list with first index

Hello, how are you, I have a little doubt how I can order the list using only the first index

the new list should be in this order

image

Add this line to your output.

output.sort(key=lambda x: x[0], reverse=True)

2 Likes

@Rickkrrd ,

it remains 0, why?

nums = [[10,"al"],[3,"Martin,"],[3,"Distrito"],[2,"cual"]]


OUT = nums.sort(key=lambda x: x[0], reverse=True)
   

I expected it to be Tuples. For lists in lists:

OUT = sorted(nums, key=lambda x: x[0], reverse=True)

2 Likes

You can just use nums.sort(reverse=True). The part you’re missing is that list.sort does not return anything, it only modifies the list. So you have to call list.sort for nums but then return nums in your OUT statement.

EDIT: The problem that I think you’re going to have is that you’re currently not considering sublists with the same key (zero index). For example, [2,"cual"] and [2,"Distrito"] are “out of order” because the sorting is respecting the initial list order.

2 Likes

@Nick_Boyts @Rickkrrd Thank you very much for all the answers