[Tutor] Sorting a dictionary by a value when the values are tuples

ranjan das ranjand2005 at gmail.com
Sat Mar 5 10:53:35 CET 2011

This is a small example i created

from operator import itemgetter

temp={'4':(2,3), '2':(5,8)}

print temp.items()

new_temp=sorted(temp.items(), key=itemgetter(1)

print new_temp

I want to sort the dictionary by the value in the first element of the tuple
((2,3) and (5,8)) and then the second value.

How do i do it? by setting key=itemgetter(1), it seems to sort the dict by
the first value of the tuple. But I am not able to do it for the second
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110305/e99e1d47/attachment.html>

More information about the Tutor mailing list