[Solved] How to sort python dictionary/list?


Assuming I understood your question, these should do it:

for person in sorted(cat1):
    print(person, max(cat1.get(person)))

result:

ben 9
jeff 6
sam 9

then:

for person in sorted(cat1, key=lambda x: max(cat1.get(x)), reverse=True):
    print(person, max(cat1.get(person)))

result:

ben 9
sam 9
jeff 6

then:

for person in sorted(cat1, key=lambda x: sum(cat1.get(x))/len(cat1.get(x)), reverse=True):
    print(person, sum(cat1.get(person))/len(cat1.get(person)))

result:

ben 7.666666666666667
sam 6.666666666666667
jeff 4.0

1

solved How to sort python dictionary/list?