Michael Watts Michael Watts - 1 year ago 138
Python Question

Max Value in a Python Dictionary

So I won't lie, this is a homework assignment. I am trying to find the max values in a dictionary:

scores = dict()
scores['Andy'] = 78
scores['Bill'] = 82
scores['Cindy'] = 94
scores['Dave'] = 77
scores['Emily'] = 82
scores['Frank'] = 94
scores['Gene'] = 87

max_val = max(scores, key=scores.get)

I need to print out both Cindy and Frank's names to show they have the max value, but it prints out only the first name (Cindy). How do I get it to print the second one as well? I'm sure this will be a very simple answer but please don't crucify me. I'm still fairly new to this.

Answer Source

You have found the maximum value. Now just compare:

max_vals = [x for x, v in scores.items() if v == scores[max_val]]

>>> ['Cindy', 'Frank']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download