<div class="gmail_quote">On Wed, Nov 23, 2011 at 7:04 AM, Cranky Frankie <span dir="ltr">&lt;<a href="mailto:cranky.frankie@gmail.com" target="_blank">cranky.frankie@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


In playing around with Pyton 3 dictionaries I&#39;ve come up with 2 questions<br>
<br>
1) How are duplicate keys handled? For example:<br>
<br>
Qb_Dict = {&quot;Montana&quot;: [&quot;Joe&quot;, &quot;Montana&quot;, &quot;415-123-4567&quot;,<br>
&quot;<a href="mailto:joe.montana@gmail.com" target="_blank">joe.montana@gmail.com</a>&quot;,&quot;Candlestick Park&quot;],<br>
&quot;Tarkington&quot;: [&quot;Fran&quot;, &quot;<a href="tel:651-321-7657" value="+16513217657" target="_blank">651-321-7657</a>&quot;, &quot;<a href="mailto:frank.tarkington@gmail.com" target="_blank">frank.tarkington@gmail.com</a>&quot;,<br>


&quot;Metropolitan Stadidum&quot;],<br>
&quot;Namath&quot;: [&quot;Joe&quot;, &quot;<a href="tel:212-222-7777" value="+12122227777" target="_blank">212-222-7777</a>&quot;, &quot;<a href="mailto:joe.namath@gmail.com" target="_blank">joe.namath@gmail.com</a>&quot;, &quot;Shea Stadium&quot;],<br>



&quot;Elway&quot;: [&quot;John&quot;, &quot;<a href="tel:303-9876-333" value="+13039876333" target="_blank">303-9876-333</a>&quot;, &quot;<a href="mailto:john.elway@gmai.com" target="_blank">john.elway@gmai.com</a>&quot;, &quot;Mile High Stadium&quot;],<br>



&quot;Elway&quot;: [&quot;Ed&quot;, &quot;<a href="tel:303-9876-333" value="+13039876333" target="_blank">303-9876-333</a>&quot;, &quot;<a href="mailto:john.elway@gmai.com" target="_blank">john.elway@gmai.com</a>&quot;, &quot;Mile High<br>


Stadium&quot;],<br>
&quot;Manning&quot;: [&quot;Archie&quot;,&quot;<a href="tel:504-888-1234" value="+15048881234" target="_blank">504-888-1234</a>&quot;, &quot;<a href="mailto:archie.manning@gmail.com" target="_blank">archie.manning@gmail.com</a>&quot;,<br>


&quot;Louisiana Superdome&quot;],<br>
&quot;Staubach&quot;: [&quot;Roger&quot;,&quot;<a href="tel:214-765-8989" value="+12147658989" target="_blank">214-765-8989</a>&quot;, &quot;<a href="mailto:roger.staubach@gmail.com" target="_blank">roger.staubach@gmail.com</a>&quot;,<br>


&quot;Cowboy Stadium&quot;]}<br>
<br>
print(Qb_Dict[&quot;Elway&quot;],&quot;\n&quot;)                        # print a dictionary entry<br>
<br>
In the above the &quot;wrong&quot; Elway entry, the second one, where the first<br>
name is Ed, is getting printed. I just added that second Elway row to<br>
see how it would handle duplicates and the results are interesting, to<br>
say the least.<br>
<br>
2) Is there a way to print out the actual value of the key, like<br>
Montana would be 0, Tarkington would be 1, etc?</blockquote><div><br></div><div>I&#39;m not sure about #1, but I can tell you about #2.</div><div><br></div><div>Dictionaries are not ordered, so you have absolutely no guarantee which order they&#39;ll appear in when you print them out, or if you iterate over the dictionary. If you want to maintain some type of order you have a few options. First, store the keys in a list, which does maintain order: </div>


<div><br></div><div>keys = [&#39;Elway&#39;, &#39;Montana&#39;, ... ]</div><div><br></div><div>Then you would do something like:</div><div><br></div><div>Qb_Dict[keys[0]]</div><div><br></div><div>(As a slight aside, I&#39;ll direct you to PEP 8 which is the Python style guide which contains things like naming conventions. If you want your code to look Pythonic, you should take a look there.)</div>


<div><br></div><div>If you just want them to be sorted, you can run sorted on the keys() collection from the dictionary:</div><div><br></div><div>for key in sorted(Qb_Dict.keys()):</div><div>    print(Qb_Dict[key])</div>

<div>
<br></div><div>In Python 3 this will only work if your collection contains comparable types - if you have {1:&#39;Hello&#39;, &#39;Goodbye&#39;:2} then you&#39;ll get a TypeError when it tries to compare 1 and &#39;Goodbye&#39;</div>


<div><br></div><div>HTH,</div><div>Wayne</div></div>