Noteworthy
Filed in General, March 30, 2008, 2:10 pmDon’t search for “python dict sort by value” since you’ll get outdated answers. As of python 2.4, the “right” way to do this is:
alist = sorted(adict.iteritems(), key=lambda (k,v): (v,k))
to get the reverse order, add on a ,reverse=True
This is the fastest way to do this and it uses the least amount of memory. Enjoy.
>>> adict = [...]