Sorting a dictionary by value
Sorting a dictionary by value is a task that I have to perform once in a while. Today I was looking for a good solution to this problem and found this blog entry. This hack is very convenient and also very fast.
from operator import itemgetter
alist = sorted(d[idx].iteritems(), key=itemgetter(1), reverse=True)
If you use itemgetter(0) you can sort by key. Should take a deeper look into the operator module.


0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home