You can use the operator.itemgetter(items) ,it returns a callable object which fetches item from its operand using _getitem_() method of the operand.
To use operator.itemgetter() use this code:
import operator
stats = {'p':4000, 'q':8000, 'r': 900}
max(stats.iteritems(), key=operator.itemgetter(1))[0]
In the above max() function, you have the “key” parameter is a function which compares elements to find the maximum between them. You may also use stats.iteritems() since it minimizes the memory use by eliminating the need for creating a new list.
Note:
In case you have:
stats = {'p':4000, 'q':8000, 'r': 900,‘s’:8000}
Then don’t expect the function to return both the maximum values ‘q’ and ‘s’,it will only return either of the two i.e, either ‘q’ or ‘s’ and not both.
Ex-
>>> import operator
>>> stats = {'p':4000, 'q':8000, 'r': 900,‘s’:8000}
>>> max(stats.iteritems(), key=operator.itemgetter(1))[0]
's’
For Python 3, replace stats.iteritems() with stats.items().
Hope this helps!