I got here looking for how to return mydict.keys()
based on the value of mydict.values()
. Instead of just the one key returned, I was looking to return the top x number of values.
This solution is simpler than using the max()
function and you can easily change the number of values returned:
stats = {'a':1000, 'b':3000, 'c': 100}
x = sorted(stats, key=(lambda key:stats[key]), reverse=True)
['b', 'a', 'c']
If you want the single highest ranking key, just use the index:
x[0]
['b']
If you want the top two highest ranking keys, just use list slicing:
x[:2]
['b', 'a']