-2

Here is the dictionary:

{'A': [2, 4.2], 
 'B': [4, 4.5],
 'C': [2, 3.3], 
 'D': [2, 3.5], }

Sorting criteria:

  1. Sort based on the 1st(first) element of list of VALUE.

  2. If the 1st element is the same for two keys, the one with the greater value of 2nd element is chosen as the greater one.

The result should be:

B -- 4 -- 4.50

A -- 2 -- 4.20

D -- 2 -- 3.50

C -- 2 -- 3.30

  • 2
    Welcome to Stack Overflow! Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a _specific question_ about your algorithm or technique. – Pranav Hosangadi Feb 09 '21 at 16:48
  • 1
    Could you add what you tried so far? Also, please elaborate on 2. I don't think I got the criteria – JetLeg Feb 09 '21 at 16:49
  • Try using a lambda with the `.sort()` function. – M-Chen-3 Feb 09 '21 at 16:51
  • And another sorted dictionary ..... why? They are insert-ordered since 3.7 - why sort them? Dicts are not supposed to be sorted. – Patrick Artner Feb 09 '21 at 16:52
  • ou might want to check [how-do-i-sort-a-dictionary-by-value](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Patrick Artner Feb 09 '21 at 16:53

1 Answers1

0

You may sort the dictionary using various methods but if your goal is to print it, you simply cannot because Dictionaries are unpredictable and the output is random, you might want to use Ordered Dictionaries and / or use the sort function to sort them.

So that we can help you better, please show us what you tried and the errors you face so that it’s easier for us to help you out.

Rajat Shenoi
  • 393
  • 1
  • 5
  • 17
  • Dictionaries are not unpredictable - they are insert ordered. The order you insert keys rules how they are outputted - this holds for any python 3.7+, some pythons 3.6+ due to implementation details. – Patrick Artner Feb 09 '21 at 17:08