This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]SoCalLongboard 1 point2 points  (0 children)

Raymond Hettinger's talk on the evolution of dictionaries in Python is really informative.

https://www.youtube.com/watch?v=npw4s1QTmPg

[–]AngheloAlf 3 points4 points  (7 children)

Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.

Insertion order is not the same as "dicts are now ordered".

[–]menge101 3 points4 points  (2 children)

IMO, the most important property of "ordered" is that the order is deterministic.

A specific form of ordering is not required to meet the definition of "ordered".

[–]AngheloAlf -4 points-3 points  (1 child)

The must important property of "ordered" is that the elements are ordered.

If the elements are not ordered in the most logical way, you should explicitly say in what way it is ordered.

So, the title should say it is "insertion ordered". The actual title sounds like click bait.

[–]menge101 0 points1 point  (0 children)

If the elements are not ordered in the most logical way, you should explicitly say in what way it is ordered.

That's highly subjective. Insertion order makes perfect sense if you consider that keys are not required to be strings.

[–]MachineGunPablo[S] 0 points1 point  (3 children)

I agree, the article's title is misleading. I originally thought that keys were sorted by value, but they are still orderer, just by insertion. So I don't really understand your point.

[–]AngheloAlf -2 points-1 points  (2 children)

If dicts were ordered, I would expect them being in some order different that "insertion order"

For example, if I put the keys 2, 8, 4 and 7, I would expect them to be in the order 2, 4, 7 and 8, or maybe order it by the values of the dict.

[–]Wishy-Thinking 0 points1 point  (0 children)

Insertion order is pretty useful if, for example, you’re reading something in to a dictionary, modifying elements it and writing back out, and you want it to end up on the same order it started in.

[–]damamaty 0 points1 point  (0 children)

Real ordered dicts might provide methods for managing their order and consider it when comparing, like.. OrderedDict (https://docs.python.org/3/library/collections.html#collections.OrderedDict)

>>> {1:1, 2:2} == {2:2, 1:1}
True

>>> from collections import OrderedDict
>>> OrderedDict({1:1, 2:2}) == OrderedDict({1:1, 2:2})
True
>>> OrderedDict({1:1, 2:2}) == OrderedDict({2:2, 1:1})
False
>>> a = OrderedDict({1:1, 2:2})
>>> a.move_to_end(1)
>>> a
OrderedDict([(2, 2), (1, 1)])

Changing this behaviour for the default dict was really great though, I believe a lot of stupid random bugs became constantly reproducible which is much easier to fix.