I have a dict d = {'a': '1', 'c': '10', 'b': '8', 'e': '11', 'g': '3', 'f': '2'}. I want to sort a dict with a numeric value d.values(). It takes ans ['a','f', 'g', 'b', 'c', 'e']. I checked here . I could not sort it by the integer value of d.values ().
d = {'a': '1', 'c': '10', 'b': '8', 'e': '11', 'g': '3', 'f': '2'}
d.values()
['a','f', 'g', 'b', 'c', 'e']
>>> 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']
This is because the values in your dictionary are of type string: pay attention to the quotation marks around the numbers. Try installing the dictionary as:
string
d = {'a':1, 'c':10, 'b':8, 'e':11, 'g':3, 'f':2}