Reputation: 19
I have this list:
victories = [[0, 7], [1, 2], [2, 3], [3, 6], [4, 7]]
I have to return all the first sub-elements, so 0, 1, 2, 3, 4
BUT
I need to sort the list according whom has the higher second sub-element, thus 7, 7, 6, 3, 2
If a first sub-element has the same second sub-element of another first sub-element, then who is lower is ranked before.
so the final list must be:
[0, 4, 3, 2, 1]
How can I do that?
Upvotes: 0
Views: 378
Reputation: 8273
You can try this one out using sorted
and sort on second element first and then first like below
victories = [[0, 7], [1, 2], [2, 3], [3, 6], [4, 7]]
[i[0] for i in sorted(victories,key=lambda x : (-x[1],x[0]))]
Upvotes: 1