sort values and return list of keys from dict python [duplicate]
Possible Duplicate:
Python: Sort a dictionary by value
I have a dictionary like this:
A = {'Name1':34, 'Name2': 12, 'Name6': 46,....}
I want a list of keys sorted by the values, i.e. [Name2, Name1, Name6....]
Thanks!!!
Solution 1:
Use sorted
with the get
method as a key (dictionary keys can be accessed by iterating):
sorted(A, key=A.get)
Solution 2:
Use sorted
's key
argument
sorted(d, key=d.get)
Solution 3:
sorted(a.keys(), key=a.get)
This sorts the keys, and for each key, uses a.get
to find the value to use as its sort value.