Differences Between Dictionary Orders In Python 3.6 And Python 3.5
Solution 1:
TLDR:
To replicate the hash-based ordering, you must take an explicitly ordered dict
and impose your own ordering.
from collections import OrderedDict
defcpy35dict(source_dict):
"""Apply hash-ordering to a dictionary"""return OrderedDict( # ensure stable orderingsorted( # order items by key hash
source_dict.items(),
key=lambda item: hash(item[0])
)
)
This replicates the ordering used in CPython up to Python 3.5. Note that the Python language specification makes no guarantees on order prior to Python 3.7 - you should also use this in Python 3.5 and before if you insist on the ordering.
There are basically three types of order for dict
-like containers:
- no order: There is no specific order to items. Every access may return a different order.
- specific order: There is a well-defined order to items. Every access returns the same order.
- arbitrary order: The ordering is not defined. It may or may not use a specific order.
As per the Python specification, dict
has arbitrary order up to Python 3.6 and insertion order since Python 3.7.
Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6. [Mapping Types — dict¶]
However, arbitrary order does not exclude specific order. It basically means "whatever the implementation feels like". Different implementations use different schemes to implement dict
.
PyPy uses insertion order since Python 2.7/3.2
CPython 3.5- uses the ordering of the underlying hash function. Since several types have salted hashes, this means you get different order on each invocation.
CPython 3.6 uses the ordering by which items are inserted. This is explicitly an implementation detail.
The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon [What's new in Python 3.6]
In other words, code for Python 3.6 and earlier should make no assumptions about ordering of dict
. Only code for Python 3.7 and later should make assumptions about ordering of dict
.
Solution 2:
The order of a dictionary is "allowed" to be different for each run of the program, let alone the version. The fact that it is consistently ordered on each version an implementation detail* that only CPython knows about. Your program should not rely on this behaviour.
How can I make my code order the result of python 3.6.7 similar to 3.5.2's.
Use an OrderedDict!
* Actually, as of Python 3.7, the preservation of insertion-order is officially a part of the language spec.
Post a Comment for "Differences Between Dictionary Orders In Python 3.6 And Python 3.5"