Why is python ordering my dictionary like so? [duplicate]

Posted on

Solving problem is about exposing yourself to as many situations as possible like Why is python ordering my dictionary like so? [duplicate] and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Why is python ordering my dictionary like so? [duplicate], which can be followed any time. Take easy to follow this discuss.

Why is python ordering my dictionary like so? [duplicate]

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.

Asked By: Teifion


Answer #1:

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).

Answered By: Konrad Rudolph

Answer #2:

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.

Answer #3:

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})
Answered By: Miles
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *