question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dict order is not kept in the same way as in CPython

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
PierreQuentelcommented, Jul 4, 2020

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 😦

2reactions
goffi-contribcommented, Jul 3, 2020

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:

the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.

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

Dictionaries preserve insertion order, meaning that keys will be produced in the same order they were added sequentially over the dictionary. Replacing an existing key does not change the order, however removing a key and re-inserting it will add it to the end instead of keeping its old place.

(the emphasis is from me, as it is the topic of this Brython bug)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found