<br><br><div class="gmail_quote">On Tue, Oct 12, 2010 at 2:46 PM, Josep M. Fontana <span dir="ltr">&lt;<a href="mailto:josep.m.fontana@gmail.com">josep.m.fontana@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br><br><div class="gmail_quote"><div><div></div><div class="h5">On Tue, Oct 12, 2010 at 8:37 PM, Joel Goldstick <span dir="ltr">&lt;<a href="mailto:joel.goldstick@gmail.com" target="_blank">joel.goldstick@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br><br><div class="gmail_quote"><div>On Tue, Oct 12, 2010 at 1:52 PM, Josep M. Fontana <span dir="ltr">&lt;<a href="mailto:josep.m.fontana@gmail.com" target="_blank">josep.m.fontana@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Thanks Chris and Alan,<br><br>OK, I see. Now that I managed to build the dictionary, I did a print to confirm that indeed the dictionary was created and it had the intended contents and I was surprised to see that the order of the items in it was totally changed. So the text file from which the dictionary was created was sorted in alphabetical order (&#39;A-01,1334&#39;, &#39;A-02,1234&#39;,...&#39;I-01,1334&#39;...), but when I print the dictionary, I get: {&#39;I-02&#39;: &#39;1399&#39;, &#39;I-01&#39;: &#39;1374&#39;, &#39;D-09&#39;: &#39;1524&#39;, &#39;I-07&#39;: &#39;1399&#39; .....}<br>



<br>I don&#39;t think this will be a problem for what I want to do next but I&#39;m curious to know why the order is all changed in a way that doesn&#39;t seem to be very intuitive.<br></blockquote></div><div><br>Here is a discussion of how to iterate over a dictionary in sorted order:<br>


 <br></div><div><a href="http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order" target="_blank">http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order</a> <br>

</div></div></blockquote></div></div><div><br><br>Thanks very much, Joel, for your quick response. This information might be useful but my question was really more out of curiosity to know how Python works. I don&#39;t understand why a dictionary that was created by iterating over a list that was sorted turns out to be unsorted. I would not have been surprised if the order was reversed, but the order that I got makes no sense to me. What is the logic of the order in which the items in this dictionary are arranged?<br>

<br>JM<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote"><div>
</div><br></div></blockquote></div></blockquote><div><br>Well, dictionaries use keys which are stored in hash tables.  A dictionary is a mapping.  It doesn&#39;t require an order.  It just requires that given a key, it will return a value.  While I don&#39;t know enough about this to be instructive, it makes looking up values faster.  When the dictionary is retrieved, its order depends on the hashed values rather than the keys themself.  If you play around with a dictionary, adding new key-value pairs, then displaying the dict, you might find that the order changes in no particular way.  Here is a wikipedia article. <br>
</div><div><a href="http://en.wikipedia.org/wiki/Hash_table">http://en.wikipedia.org/wiki/Hash_table</a> <br><br>A list, on the other hand IS ordered (its a squence), and so when you add or remove values from a list, the order is maintained.<br>
<br></div></div>-- <br>Joel Goldstick<br><br>