0

I've the following code:

tweet = {"user" : "joelgrus", "text" : "Data Science is Awesome", "retweet_count" : 100}

print "tweet:", tweet

The output after running the script is:

tweet: {'text': 'Data Science is Awesome', 'retweet_count': 100, 'user': 'joelgrus'}

Why is the order of the elements (seemingly) wrong? I thought the output should just be:

tweet: {'user': 'joelgrus', 'text': 'Data Science is Awesome', 'retweet_count': 100}
Hunter
  • 357
  • 8
  • 15
  • 3
    Dictionaries are inherently unordered – jDo Apr 19 '16 at 01:08
  • 1
    Also see [here](http://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary). – TigerhawkT3 Apr 19 '16 at 01:09
  • 3
    I recommend Googling your planned question title before posting it, as that may sometimes provide an immediate answer to your question. – TigerhawkT3 Apr 19 '16 at 01:11
  • @TigerhawkT3 yeah you're right, I'm sorry for asking this question, I just assumed that dictionaries are ordered (since that seems to such a logic thing). I would delete the question, but I believe that's frowned upon as well. – Hunter Apr 19 '16 at 01:14

1 Answers1

4

Dictionaries do not keep their lists in order: https://docs.python.org/2/library/stdtypes.html#typesmapping

Theres a note on the page that explains:

CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

MikeTGW
  • 395
  • 2
  • 10