Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

11–20 of 457 posts

Re: Python dicts are now ordered

#11
post #2

"Ordered dict" is ambiguous, and from the title I thought that key order was meant. Reading the article, I see that it's actually insertion order. Which makes much more sense. Key order would have been a much more significant change.

Not to mention there's often no meaningful way to order by key value since keys can be any hashable values -- so things like this break this idea:

    _dict = {}
    _dict.update({MyObject: 3})
    _dict.update({'MyObject': 3})
There's no ordering over _most_ hashable values since they span multiple types, so insertion ordering is the only sane way to do it.

Re: Python dicts are now ordered

#12
post #3

Javascript taught me to never assume any ordering in a dict/map/hashmap (whatever the term)

That's fine, as long as you adjust your assumptions when working with other languages.

It's a safe default, if maybe not a performant one.

Re: Python dicts are now ordered

#13
post #3

Javascript taught me to never assume any ordering in a dict/map/hashmap (whatever the term)

That's fine, as long as you adjust your assumptions when working with other languages.

Holding as little assumptions as possible works as a general rule no matter which language you're using.

Re: Python dicts are now ordered

#14
post #3

Javascript taught me to never assume any ordering in a dict/map/hashmap (whatever the term)

These collections are often default defined as unordered collections; it’s just that people discovered over time that some were iterable even if the language docs said otherwise.

Re: Python dicts are now ordered

#15
Am I the only one that thinks this is a stupid decision? This will silently break code that starts to rely on this behaviour that gets executed on Python3.5 and lower. I would consider changing how a builtin works to be a major breaking change. It would have been fine if this was a change between 2 and 3 but on a minor version? Thats insane.

Re: Python dicts are now ordered

#16
post #2

"Ordered dict" is ambiguous, and from the title I thought that key order was meant. Reading the article, I see that it's actually insertion order. Which makes much more sense. Key order would have been a much more significant change.

Not to mention there's often no meaningful way to order by key value since keys can be any hashable values -- so things like this break this idea: _dict = {} _dict.update({MyObject: 3}) _dict.update({'MyObject': 3}) There's no ordering over _most_ hashable values since they span multiple types, so insertion ordering is the only sane way to do it.

Well, keys of sorted dictionaries don’t need to be hashable but do need an ordering, whether intrinsic or user-provided. A sorted map (usually implemented in terms of a binary search tree) is simply a separate data type with different requirements than a hash table based one.

Re: Python dicts are now ordered

#19
post #15

Am I the only one that thinks this is a stupid decision? This will silently break code that starts to rely on this behaviour that gets executed on Python3.5 and lower. I would consider changing how a builtin works to be a major breaking change. It would have been fine if this was a change between 2 and 3 but on a minor version? Thats insane.

Go purposely "randomizes" order of a map when it's iterated over, which you can see running this demo:

https://play.golang.org/p/DISpyv0Zuq_j

HN discussed this a little 6 years ago: https://news.ycombinator.com/item?id=7655948

As crawshaw pointed in that discussion, it helps catch people inadvertently relying on map order in tests or other places in their code.

Re: Python dicts are now ordered

#20
post #15

Am I the only one that thinks this is a stupid decision? This will silently break code that starts to rely on this behaviour that gets executed on Python3.5 and lower. I would consider changing how a builtin works to be a major breaking change. It would have been fine if this was a change between 2 and 3 but on a minor version? Thats insane.

I'm not clear on how this break existing code.

Code that assumed it was arbitrary, would expect to handle any arbitrary order, including a happens-to-be sorted order.

Code that assumed it was random, like actually inserted by random(), was already broken, because that simply isn't the case.

Code that assumed the order would stay constant was relying on implementation-specific behavior, and could potentially break on any version update; as with any reliance on implementation-specific behavior, you'd break if the dictionary code ever got touched -- even if it were for a bugfix.

Code that ordered the dictionary keys before iterating are now slightly innefficient due to extra work of sorting a sorted list.

Post reply on HN