d = {'apple':9,'oranges':3,'grapes':22}
How do I return the largest key/value?
Edit: How do I make a list that has this sorted by largest to lowest value?
>>> d = {'apple':9,'oranges':3,'grapes':22}
>>> v, k = max((v, k) for k, v in d.items())
>>> k
'grapes'
>>> v
22
Edit: To sort them:
>>> items = sorted(((v, k) for k, v in d.items()), reverse=True)
>>> items
[(22, 'grapes'), (9, 'apple'), (3, 'oranges')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With