Reputation: 7297
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().
Upvotes: 5
Views: 9705
Reputation: 1265
That's because the values in your dictionary are of type string
: note the quotes around the numbers. Try setting your dictionary as:
d = {'a':1, 'c':10, 'b':8, 'e':11, 'g':3, 'f':2}
Upvotes: 0
Reputation: 32300
>>> 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']
Upvotes: 12