I have a list that looks like this: my_list = [('a', 4), ('z', 3), ('y', 3), ('x', 3), ('p', 2), ('o', 2)]
.
It is important that the tuples in the list are sorted by the second element ([1]) in the descending order.
Now I need to sort each group of tuples with the same second element in the ascending order based on the first element ([0]) within each group. In other words, I need to get the list
my_new_list= [('a', 4), ('x', 3), ('y', 3), ('z', 3), ('o', 2), ('p', 2)]
.
How can I write the sorted function with the proper lambda? I can't write just my_list.sort(key=lambda item: item[0])
because it will break the descending order of the tuples when they are sorted by their second elements.
What should my lambda look like?