2

I created a dictionary, having 3 keys and each of the key had single string as it's data.

After it's creation, just to verify it, i printed it out just to find that the order of the keys is different than how i entered them.

It would have made sense if python was arranging the keys in some incremental or decremental order, however in this case it' hard to believe this either. Nor this sounds too cool on the justification of the size of data associated with that key.

Why is python arranging the keys out of his own mind??

From some other discussion I did find that: Dictionaries are unsorted. The order they display in is due to internal logic, and won't correspond to the order you define them in.

However I am more interested in what is happening inside, and what is the internal logic what causes this; tough knowing the fact that dictionaries are accessed by keys and not indices so it hardly makes any difference in what order the items are.

Here's the output window of my shell: enter image description here

BhaveshDiwan
  • 669
  • 10
  • 22
  • 1
    Dictionaries are inherently unordered. It's part of what makes them very efficient at what they do. [Here](http://docs.python.org/2/tutorial/datastructures.html#dictionaries) are the docs where you can read more. – KobeJohn Nov 11 '13 at 00:37
  • If you want to know how they work, you can change the title to represent that. However, it's been asked. [Here](http://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented) is a great set of answers. – KobeJohn Nov 11 '13 at 00:41
  • @kobejohn: I think he's asking how is it represented when it is strigified, and how the representation has any particular order for that. – Qantas 94 Heavy Nov 11 '13 at 01:42

1 Answers1

0

After 6 years and 9 months of remaining unanswered, I'd like to answer and close this question myself.

  1. As remarked by @KobeJohn, the satisfactory answer is "Python's dictionaries are inherently unordered.
  2. When stringified, the representation has nothing to do with how it's stored.
  3. Refer to the official documentation for more information.
  4. Implementation of dictionaries can be learned by this StackOverflow post.

NOTE: This answer's focus is purely on python v2.7. Theoretically, this should be obsolete now with the advent of Python 3.x. This has been answered now just for sake of answering & closing it.

BhaveshDiwan
  • 669
  • 10
  • 22