-4

Using Python, If I have this list:

cars = {'Honda': 'Civic', 'Audi': 'A4', 'Chevrolet': 'Camaro', 'Volkswagen': 'Passat', 'Jeep': 'Wrangler', 'Pontiac': 'G6'}

Python 2.6 prints it like this:

print(cars)
{'Pontiac': 'G6', 'Jeep': 'Wrangler', 'Chevrolet': 'Camaro', 'Honda': 'Civic', 'Volkswagen': 'Passat', 'Audi': 'A4'}

Python 3.3 prints it like this:

print(cars)
{'Jeep': 'Wrangler', 'Honda': 'Civic', 'Pontiac': 'G6', 'Chevrolet': 'Camaro', 'Volkswagen': 'Passat', 'Audi': 'A4'}

How does Python determine the order of the printed items? This is by default, without sorting the list first. Why doesn't either version print the list as is? How is this accomplished?

jramirez
  • 8,537
  • 7
  • 33
  • 46
mindmischief
  • 271
  • 1
  • 12
  • 3
    First, that is a dictionary, not a list. Second, dictionaries are not ordered, so you can't assume anything about the order. You can find probably hundreds of duplicate questions about this on this site. – BrenBarn Jan 13 '14 at 18:27
  • You were downvoted because of the lack of research effort, but in my opinion this is a good catch for a beginner. Keep it up. Also, BrenBarn answered your question. – keyser Jan 13 '14 at 18:28
  • 1
    If you really want to know why, you could do worse than watch [this](http://blip.tv/pycon-us-videos-2009-2010-2011/pycon-2010-the-mighty-dictionary-55-3352147). – jonrsharpe Jan 13 '14 at 18:35

4 Answers4

1

The best answer to this is: "Don't bother with the order of a dictionary."

Dictionaries are not ordered types. They use hashes to store the values, and are not sortable (e.g. if you call sorted(dict), it returns a list). Don't assume anything about a dictionary by the order it's in.

If you need to have a stable order, use:

from collections import OrderedDict

cars = OrderedDict({'Honda': 'Civic', 'Audi': 'A4', 'Chevrolet': 'Camaro',\
                  'Volkswagen': 'Passat', 'Jeep': 'Wrangler', 'Pontiac': 'G6'})
#The order is now structured. You can sort it and use it as if it were ordered.
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • I'd say the best is: _Dictionaries are unordered which means that no specific order is guaranteed, not even insertion order_. Or something like that. – keyser Jan 13 '14 at 18:30
0

From the tutorial:

It is best to think of a dictionary as an unordered set of key: value pairs...

The key there is unordered. You can get a sorted list of the keys in a dictionary with sorted(cars). Note that this doesn't change the dictionary to a sorted type. It returns a list of just the keys.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
0

The default dictionary prints the keys in a random fashion so you are never know how they will be printed. If you want the keys to be printed in order you can use an OrderedDict.

jramirez
  • 8,537
  • 7
  • 33
  • 46
  • As I recall, at least Python3 prints dictionaries in a stable sort based on their hash values, but it doesn't MEAN anything.... – Adam Smith Jan 13 '14 at 18:34
-1

Dicts are hashmaps, and order of printing depends on order of iteration which is random, and is controlled by hashes of keys.

I doubt that there is difference in way different python versions compute str hashes, more likely initial size of dict is different, so hash(key) % size_of_hashtable is different, so keys are ordered differently.

PS. adsmith is right - dont bother with dict's order.

== EDIT ==

I tried checking with p2/p3 CPython implementation sources, and to be frank I'm quite lost (I DONT like C :P ). Dicts have evolved a lot between those version, and there were some experiments with caching, initial sizes, etc.

In the end, full answer is "because implementation of dict object varies between those versions".

Filip Malczak
  • 3,124
  • 24
  • 44
  • I get it, that someone found this answer wrong, but I'd like to know why (for me to avoid giving wrong answers in the future). – Filip Malczak Jan 13 '14 at 18:42