Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

name nomadic dead port realOwner secret slaves team y x population id description 

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

like image 631
Teifion Avatar asked Feb 08 '09 18:02

Teifion


People also ask

Is dictionary duplicate in Python?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.

Does Python dictionary keep order?

Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.

Does dictionary maintain insertion order?

From Python 3.6 onwards, the standard dict type maintains insertion order by default. will result in a dictionary with the keys in the order listed in the source code.

Can dictionary contain duplicates?

[C#] Dictionary with duplicate keysThe Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.

How to implement an unordered dictionary in Python?

— An unordered dictionary is usually implemented as a hash tablewhere 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.

What is the Order of Python dictionaries?

more precise: The ordering of python dictionaries is arbitary but deterministic (according to the python spec). Where deterministic means it will always behave the same way.

Is it possible to reorder dictionary keys in Python?

This is quite popular problem, with the advent of newer version of Python, where keys are ordered in Dictionaries, there might be requirement to reorder dictionary keys. Let’s discuss certain ways in which this task can be performed.

How to construct a newer dictionary in Python?

In this, we construct the newer dictionary by appending the keys in order list, mapping with keys in original dictionary. Works with Python >= 3.6. This is yet another way in which this task can be performed.


Video Answer


2 Answers

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

like image 76
Konrad Rudolph Avatar answered Sep 25 '22 05:09

Konrad Rudolph


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.

like image 25
Christian C. Salvadó Avatar answered Sep 21 '22 05:09

Christian C. Salvadó