I am trying to sort a dictionary, the order which I want to follow is that first, the dictionary should be sorted in increasing order by values and if the values for two or more keys are equal then I want to sort the dictionary by the keys in descending order.
Here is the code:
dictionary = {0: 150, 1: 151, 2: 150, 3: 101, 4: 107}
print(sorted(dictionary.items(), key=lambda x: (x[1], x[0])))
I want the output to be the following:
[(3, 101), (4, 107), (2, 150), (0, 150), (1, 151)]
But the output is:
[(3, 101), (4, 107), (0, 150), (2, 150), (1, 151)]