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.
The straight answer is NO. You can not have duplicate keys in a dictionary in Python.
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.
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.
[C#] Dictionary with duplicate keysThe Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.
— 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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With