Sort the values of a dict

Rhodri James rhodri at wildebst.demon.co.uk
Sat Dec 19 20:18:40 EST 2009


On Fri, 18 Dec 2009 23:00:42 -0000, David Robinow <drobinow at gmail.com>  
wrote:

> I won't engage in any arguments about pythonicity but it seems simpler
> if you convert to a list of tuples right away.
>
> d = {1:('a', 1, 12), 5:('r',21,10), 2:('u',9,8)}
> l = [(x, d[x]) for x in d.keys()]

which is a long way of writing "l = d.items()", or "l = list(d.items())"  
if you're using Python 3. :-)

> def third(q):
>     return q[1][2]
>
> s = sorted(l, key=third)
> print s


-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list