How do I sort this list of lists in that matches two conditions?

Viewed 30

I have a list of lists that looks like this [[value, frequency], ... , [value, frequency]]. I wish to sort it in:

  1. descending order by frequency
  2. if two frequencies have the same value then I want to sort those lists in ascending order by value.

Example: [[7,1], [8,2], [4,3], [3,2] --sorted--> [[4,3], [3,2], [8,2], [7,1]]

I've tried sorting the list in descending order by frequency like follows: sorted(list, key=lambda x:x[1], reverse=True) but this doesn't take care of the second condition Appreciate the help!

0 Answers
Related