-1

Possible Duplicate:
In what order does python display dictionary keys?

There was this very weird Python question which I was trying to answer (on SO but looks like the author deleted it). This has gotten me head-over-heels and I cannot seem to plausibly explain myself what could be the cause behind this flummoxing phenomenon. I have a Python dictionary (associative array) which I initialize as below.

d = {"word1": 1, "word2": 2, "word3": 3}

And loop through it using a for..in construct in which i print the keys of "d" separated by a space. Expected output: word1 word2 word3
Actual output: word1 word3 word2

I then tried to print "d" as

 print d 

Output

{'word1': 1, 'word3': 3, 'word2': 2}

I was scanning the online Python docs, existing questionnaire on SO, blogs but fully failed to figure out the cause. Am I missing something here? Furthermore, there's a kind request to all those who down-vote this question to please elaborate on the reason for the same.

Cheers. :-)

Community
  • 1
  • 1
verisimilitude
  • 5,077
  • 3
  • 30
  • 35
  • 3
    -1. Elaboration: You dance around the subject, but you never say what it is that's wrong with the expected output vs the actual output. The problem is in the *order*. Google or search in stackoverflow "python", "order", "dictionary" and the answer is found pretty quickly. – Steven Rumbalski May 22 '12 at 15:31
  • 1
    I'm voting to close this question as an exact duplicate. Utmost thanks to all who cared to post/comment here. :) – verisimilitude May 22 '12 at 17:07

3 Answers3

5

Python dict does not guarantee any particular ordering of its keys.

collections.OrderedDict preserves the insertion order of its keys.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

The keys in a Python dictionary are not sorted.

for key in sorted(d):
    print key,

prints word1 word2 word3 as expected.

eumiro
  • 207,213
  • 34
  • 299
  • 261
4

From the documentation:

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). To check whether a single key is in the dictionary, use the in keyword.

http://docs.python.org/tutorial/datastructures.html

The last paragraph says it all.

Community
  • 1
  • 1
pcalcao
  • 15,789
  • 1
  • 44
  • 64