Why does Python change the order of tuple elements returned in calls to items() and keys() on a dict?
-
Hi guys. I am currently learning Python, and I have found a weird problem with lists and dictionaries, maybe you can help me nail it down: Code below: ------------------------------------------------------------------------------------- women = {"Cute":"Alyssa", "Beatufiul":"Sayana", #ignore the typo in "Beautuful". "Happy":"Boni"} print women #prints the whole dictionary. print loliDic.keys() #Prints the keys of the dictionaries in a list. print loliDic.values() #Prints the values of each key in a list. #The problem starts below... hmm = women.items() #Assigns "hmm" a list of touples of the key-value relationship of each element in my dictionary. print hmm[0] ----------------------------------------------------------------------------------- Now, you may have seen that I am trying to print hmm[0] which works BTW, but I don't think its working as it should. I was expecting that: * hmm[0] would print ("Cute", "Alyssa") * hmm[1] would print ("Beautuful", "Sayana") * hmm[2] would print ("Happy", "Boni") But instead: * hmm[0] prints ("Beautuful", "Sayana") * hmm[1] prints ("Cute", "Alyssa") * hmm[2] prints ("Happy", "Boni") - This one being the only right value. Can someone tell me why is that happening? I keep analyzing my code but I can't find a single logical reason for it to print the touples in that weird order.
-
Answer:
Short answer: Python dictionaries are not ordered, and you should not expect any particular order to come out when you call items() or keys(). Longer answer: Python dictionaries are hash tables, which are not ordered. When you insert elements into the hash table, each key is hashed (using hash()) to a hash value. Here, hash() only depends on the key, so regardless of the order in which the items are inserted, they are stored in the same locations in the hash table. Then, items() or keys() returns the items in whatever order they happen to be stored in.
Yoyo Zhou at Quora Visit the source
Other answers
Yoyo's answer is correct. This talk will explain in more detail what's going on under the hood: http://python.mirocommunity.org/video/1591/pycon-2010-the-mighty-dictiona And here's the dictionary source and the notes describing its implementation, respectively: http://svn.python.org/projects/python/trunk/Objects/dictobject.c http://bugs.python.org/file6941/dictnotes.txt
Vaibhav Mallya
collections.OrderedDict (new in py3.1) is a dictionary that remembers the order in which the keys were added. Internally, I believe, it just maintains a dict and a list. For py < 3.1, the following code does the same: http://code.activestate.com/recipes/576693/
Abhik Shah
Related Q & A:
- How can you change the order of your posts?Best solution by Stack Overflow
- Why does iTunes change the order of songs in a playlist?Best solution by Yahoo! Answers
- Can I modify the keys in a dictionary directly for Python?Best solution by stackoverflow.com
- Why are the keys on a calculator opposite to the keys on a phone?Best solution by Yahoo! Answers
- How do I change the order of the songs on my iTunes playlist?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.