Initially, a list of tuples is sorted by their second value and their third value.
tlist = [('a', 1, 14), ('a', 1, 16), ('b', 1, 22),
('a', 2, 1), ('c', 2, 9), ('d', 2, 11), ('d', 2, 12)]
Trying to sort this list of tuples by their second value, reverse by their third value; that is, want the output of the list
to be sorted like so:
tlist= [('b', 1, 22), ('a', 1, 16), ('a', 1, 14),
('d', 2, 12), ('d', 2, 11), ('c', 2, 9), ('a', 2, 1)]
This is what I have tried so far as seen in this answer:
tlist = sorted(tlist, key=lambda t: return (t[0], t[1], -t[2]))
but it does not work; gives a return outside of function
error.
Any ides of how to get the desired output?
EDIT
This question provides very good resources and perhaps all someone would need to go ahead and tackle my specific question. However, given my low level of experience and my specific instance of sorting with different sorting criteria (as described on the description above), I think this is no duplicate.