Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!!

like image 617
Alejandro Avatar asked Sep 07 '11 20:09

Alejandro


1 Answers

Use sorted with the get method as a key (dictionary keys can be accessed by iterating):

sorted(A, key=A.get) 
like image 53
phihag Avatar answered Oct 02 '22 15:10

phihag