<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2658.2">
<TITLE>RE: Sorting dictionary by 'sub' value</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>[Rory Campbell-Lange]</FONT>
</P>

<P><FONT SIZE=2>#- I have a dictionary of images. I wish to sort the dictionary 'v' by a</FONT>
<BR><FONT SIZE=2>#- dictionary value using python 2.3. The dictionary value is the date</FONT>
<BR><FONT SIZE=2>#- attribute as shown here:</FONT>
</P>

<P><FONT SIZE=2>You have to use the DSU algorithm (Decorate, Sort, Undecorate), taking in consideration that the final sorted element can not be a dictionary. For example (with a simpler dictionary):</FONT></P>

<P><FONT SIZE=2>>>> d = {'a': ('m', 5), 'b': ('k', 9), 'c': ('f', 2)}</FONT>
</P>

<P><FONT SIZE=2>I want the dictionary values sorted by the number inside the tuple. So I prepare a list with that number first, and the dictionary key in second place:</FONT></P>

<P><FONT SIZE=2>>>> temp_list = [ (x[1][1], x[0]) for x in d.items() ]</FONT>
</P>

<P><FONT SIZE=2>Then, just sort it and use it.</FONT>
</P>

<P><FONT SIZE=2>>>> temp_list.sort()</FONT>
<BR><FONT SIZE=2>>>> for (tmp, key) in temp_list:</FONT>
<BR>        <FONT SIZE=2>print d[key]</FONT>
<BR>        
<BR><FONT SIZE=2>('f', 2)</FONT>
<BR><FONT SIZE=2>('m', 5)</FONT>
<BR><FONT SIZE=2>('k', 9)</FONT>
<BR><FONT SIZE=2>>>> </FONT>
</P>
<BR>

<P><FONT SIZE=2>.    Facundo</FONT>
</P>

<P><FONT SIZE=2>Bitácora De Vuelo: <A HREF="http://www.taniquetil.com.ar/plog" TARGET="_blank">http://www.taniquetil.com.ar/plog</A></FONT>
<BR><FONT SIZE=2>PyAr - Python Argentina: <A HREF="http://pyar.decode.com.ar/" TARGET="_blank">http://pyar.decode.com.ar/</A></FONT>
</P>
<BR>

<P><B><FONT SIZE=2>  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .</FONT></B></P>

<P><B><U><FONT SIZE=2>ADVERTENCIA.</FONT></U></B>
</P>

<P><B><FONT SIZE=2>La información contenida en este mensaje y cualquier archivo anexo al mismo, son para uso exclusivo del destinatario y pueden contener información confidencial o propietaria, cuya divulgación es sancionada por la ley.</FONT></B></P>

<P><B><FONT SIZE=2>Si Ud. No es uno de los destinatarios consignados o la persona responsable de hacer llegar este mensaje a los destinatarios consignados, no está autorizado a divulgar, copiar, distribuir o retener información (o parte de ella) contenida en este mensaje. Por favor notifíquenos respondiendo al remitente, borre el mensaje original y borre las copias (impresas o grabadas en cualquier medio magnético) que pueda haber realizado del mismo.</FONT></B></P>

<P><B><FONT SIZE=2>Todas las opiniones contenidas en este mail son propias del autor del mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones Personales S.A. o alguna empresa asociada.</FONT></B></P>

<P><B><FONT SIZE=2>Los mensajes electrónicos pueden ser alterados, motivo por el cual Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación cualquiera sea el resultante de este mensaje.</FONT></B></P>

<P><B><FONT SIZE=2>Muchas Gracias.</FONT></B>
</P>

</BODY>
</HTML>