dict order is not kept in the same way as in CPython
See original GitHub issueHello,
dict order after an update is inconsistent with CPython. Here is the result I have with CPython:
In [2]: a={1:1, 2:2}
In [3]: a
Out[3]: {1: 1, 2: 2}
In [4]: a.update({1:3})
In [5]: a
Out[5]: {1: 3, 2: 2}
In [6]: a.update({4:4})
In [7]: a
Out[7]: {1: 3, 2: 2, 4: 4}
The same thing with Brython:
Brython 3.8.9 on Netscape 5.0 (X11)
>>> a = {1:1, 2:2}
>>> a
{1: 1, 2: 2}
>>> a.update({1:3})
>>> a
{2: 2, 1: 3}
>>> a.update({4:4})
>>> a
{2: 2, 1: 3, 4: 4}
>>>
As you can see, the updating of key 1
is moving the key to the end, while is should keep its original location (first).
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
python - How to keep keys/values in same order as declared?
python dictionaries are unordered. If you want an ordered dictionary, try collections.OrderedDict. Note that OrderedDict was introduced into the ...
Read more >OrderedDict vs dict in Python: The Right Tool for the Job
Since Python 3.6, regular dictionaries have kept their items in the same order that they were inserted into the underlying dictionary. This limits...
Read more >Python Dictionaries Are Now Ordered. Keep Using OrderedDict.
Until recently, Python dictionaries did not preserve the order in which items were added to them. For instance, you might type {'fruits': ...
Read more >Dictionaries preserve ordering - Python Insight
Good question. Actually, no. The thing with an OrderedDict is that ordering is assumed to have meaning over and above the values stored....
Read more >Dicts are now ordered, get used to it - Ivan Sagalaev
Python has had ordered dicts for a long time (in "containers.ordereddict" I think). The change described above, in Python 3.6 (from 2016) merely ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the bug report, it was the same with string keys.
By the way, making what was a CPython implementation detail a language feature forced us, poor maintainers of alternative implementations, to rewrite the whole code for dictionaries 😦
This StackOverflow link is outdated, it predates Python 3.6
I don’t think there is any PEP for that, it’s a side effect of dict optimisation for Python 3.6, made official in Python 3.7, you can check changelog of Python 3.7 for that:
cf. Guido message at https://mail.python.org/pipermail/python-dev/2017-December/151283.html
It’s also clear in the language reference: https://docs.python.org/3/reference/datamodel.html#index-30
(the emphasis is from me, as it is the topic of this Brython bug)