Maximum key value from dictionary

Viewed 117

I have a python dictionary with values

d = {'A': 0, 'B': 1, 'C': 0, 'D': 4}

result = max(d.iteritems(), key=lambda x: x[1])

result = ('D', 4)

Now if there is no maximum value and all values are equal, then result should be by alphabetical order (ascending) of keys.

ie

 d = {'A': 0, 'B': 1, 'C': 0, 'D': 1}
 result should be  D

 d = {'A': 0, 'B': 5, 'C': 5, 'D': 1}
 result should be C

How this can be done in Python ?

1 Answers
Related