-1

so I am a beginner with Python and face one problem. The problem is that I have the following dict:

topperformer1 = {"Olivia":19, "Maria":18, "Pedro":15, "Nuno":17, "Julia":19, "Vasco":17, "Carla":18}

Now I would like to print the 3 highest grades (value), but if I sort the dict after the value and print it, I only get the value with no key.

I hope you can help me and best regards

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
Newbie
  • 25
  • 1
  • 5

1 Answers1

0

Use collections.Counter

Ex:

from collections import Counter

topperformer1 = Counter({"Olivia":19, "Maria":18, "Pedro":15, "Nuno":17, "Julia":19, "Vasco":17, "Carla":18})
print(topperformer1.most_common(3))
# --> [('Olivia', 19), ('Julia', 19), ('Maria', 18)]
Rakesh
  • 81,458
  • 17
  • 76
  • 113