Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a dict by numeric value of dict.values

I have a dict d = {'a': '1', 'c': '10', 'b': '8', 'e': '11', 'g': '3', 'f': '2'}. I want to sort the dict with numeric value of d.values(). Required ans is ['a','f', 'g', 'b', 'c', 'e']. I had checked here . I couldn't make it to sort according to integer value of d.values().

like image 263
Netro Avatar asked Mar 06 '13 09:03

Netro


1 Answers

>>> d = {'a': '1', 'c': '10', 'b': '8', 'e': '11', 'g': '3', 'f': '2'}
>>> sorted(d, key=lambda i: int(d[i]))
['a', 'f', 'g', 'b', 'c', 'e']
like image 154
Volatility Avatar answered Sep 30 '22 08:09

Volatility