<div class="gmail_quote">On Wed, Nov 23, 2011 at 7:04 AM, Cranky Frankie <span dir="ltr"><<a href="mailto:cranky.frankie@gmail.com" target="_blank">cranky.frankie@gmail.com</a>></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've come up with 2 questions<br>
<br>
1) How are duplicate keys handled? For example:<br>
<br>
Qb_Dict = {"Montana": ["Joe", "Montana", "415-123-4567",<br>
"<a href="mailto:joe.montana@gmail.com" target="_blank">joe.montana@gmail.com</a>","Candlestick Park"],<br>
"Tarkington": ["Fran", "<a href="tel:651-321-7657" value="+16513217657" target="_blank">651-321-7657</a>", "<a href="mailto:frank.tarkington@gmail.com" target="_blank">frank.tarkington@gmail.com</a>",<br>
"Metropolitan Stadidum"],<br>
"Namath": ["Joe", "<a href="tel:212-222-7777" value="+12122227777" target="_blank">212-222-7777</a>", "<a href="mailto:joe.namath@gmail.com" target="_blank">joe.namath@gmail.com</a>", "Shea Stadium"],<br>
"Elway": ["John", "<a href="tel:303-9876-333" value="+13039876333" target="_blank">303-9876-333</a>", "<a href="mailto:john.elway@gmai.com" target="_blank">john.elway@gmai.com</a>", "Mile High Stadium"],<br>
"Elway": ["Ed", "<a href="tel:303-9876-333" value="+13039876333" target="_blank">303-9876-333</a>", "<a href="mailto:john.elway@gmai.com" target="_blank">john.elway@gmai.com</a>", "Mile High<br>
Stadium"],<br>
"Manning": ["Archie","<a href="tel:504-888-1234" value="+15048881234" target="_blank">504-888-1234</a>", "<a href="mailto:archie.manning@gmail.com" target="_blank">archie.manning@gmail.com</a>",<br>
"Louisiana Superdome"],<br>
"Staubach": ["Roger","<a href="tel:214-765-8989" value="+12147658989" target="_blank">214-765-8989</a>", "<a href="mailto:roger.staubach@gmail.com" target="_blank">roger.staubach@gmail.com</a>",<br>
"Cowboy Stadium"]}<br>
<br>
print(Qb_Dict["Elway"],"\n") # print a dictionary entry<br>
<br>
In the above the "wrong" 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'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'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 = ['Elway', 'Montana', ... ]</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'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:'Hello', 'Goodbye':2} then you'll get a TypeError when it tries to compare 1 and 'Goodbye'</div>
<div><br></div><div>HTH,</div><div>Wayne</div></div>