By Teifion

2009-02-08 18:03:46 8 Comments

Here is the dictionary I have

propertyList = {
    "id":           "int",
    "name":         "char(40)",

    "team":         "int",
    "realOwner":    "int",

    "x":            "int",
    "y":            "int",

    "description":  "char(255)",

    "port":         "bool",
    "secret":       "bool",
    "dead":         "bool",
    "nomadic":      "bool",

    "population":   "int",
    "slaves":       "int",

But when I print it out with "\n".join(myDict) I get this


I know that a dictionary is unordered but it comes out the same every time and I've no idea why.


@Konrad Rudolph 2009-02-08 18:06:42

For older versions of Python, the real question should be “why not?” — An unordered dictionary is usually implemented as a hash table where the order of elements is well-defined but not immediately obvious (the Python documentation used to state this). Your observations match the rules of a hash table perfectly: apparent arbitrary, but constant order.

Python has since changed its dict implementation to preserve the order of insertion, and this is guaranteed as of Python 3.7. The implementation therefore no longer constitutes a pure hash table (but a hash table is still used in its implementation).

@llimllib 2009-02-09 06:05:37

It's well worth reading a comment from python's dictionary source file; I've already posted it in a stackoverflow post about "the best comment you've ever seen":

@Maciej Gol 2013-10-02 09:07:09

A bit of dict's internal implementation can be found here . There was a nice presentation from one of python's coders, but can't find it at the moment (I think it was from PyCon Australia).

@CMS 2009-02-08 18:14:50

The specification for the built-in dictionary type disclaims any preservation of order, it is best to think of a dictionary as an unordered set of key: value pairs...

You may want to check the OrderedDict module, which is an implementation of an ordered dictionary with Key Insertion Order.

@Miles 2009-02-10 09:37:21

The only thing about dictionary ordering you can rely on is that the order will remain the same if there are no modifications to the dictionary; e.g., iterating over a dictionary twice without modifying it will result in the same sequence of keys. However, though the order of Python dictionaries is deterministic, it can be influenced by factors such as the order of insertions and removals, so equal dictionaries can end up with different orderings:

>>> {1: 0, 2: 0}, {2: 0, 1: 0}
({1: 0, 2: 0}, {1: 0, 2: 0})
>>> {1: 0, 9: 0}, {9: 0, 1: 0}
({1: 0, 9: 0}, {9: 0, 1: 0})

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 2847960 View
  • 2683 Score
  • 16 Answer
  • Tags:   python dictionary

9 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

10 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

21 Answered Questions

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

15 Answered Questions

[SOLVED] Add new keys to a dictionary?

39 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

56 Answered Questions

[SOLVED] Calling an external command in Python

27 Answered Questions

[SOLVED] What is the best way to iterate over a dictionary?

  • 2008-09-26 18:20:06
  • Jake Stewart
  • 1355272 View
  • 2253 Score
  • 27 Answer
  • Tags:   c# dictionary loops

Sponsored Content