-4

I am very new to python and I would like to sort all the maximum scores into descending order. (There is a dictionary that stores the data for each student) At the moment it only prints the maximum scores of each student in the order the data is entered:

Maximum score
Adam Watts 7
Henry Lloyd 10
Lucy Smith 9   

This is the code I am using:

print("Maximum score")
for key in keys:
    print(key, max(Classes[key]))
martineau
  • 119,623
  • 25
  • 170
  • 301
ck_23
  • 1
  • 1

1 Answers1

0

Assuming your dictionary is like this:

students = {'Adam Watts': [3, 7], 'Henry Lloyd': [10, 1], 'Lucy Smith': [9, 9]}

You cannot sort a dictionary (entries in a dictionary have no particular order), but your can store the data that you care about in a list, and sort this list.

from operator import itemgetter
items = [(student, max(scores)) for student, scores in students.items()]
items.sort(key=itemgetter(1), reverse=True)

for student, score in items:
    print(student, score)
eskaev
  • 1,108
  • 6
  • 11