Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

41–50 of 457 posts

Re: Python dicts are now ordered

#41
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.

It's not ambiguous, because it refers to the long pre-existing OrderedDict class.

Re: Python dicts are now ordered

#42
I still see dict ordering as an implementation detail; not a technical one but a descriptive one. If you want to rely on insertion order, use collections.OrderedDict. It communicates your intention far better, and there should be no overhead.

Re: Python dicts are now ordered

#43
post #20

Earlier quoted context omitted.

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…

It doesn’t break existing code. Code written for Python 3.7 might break on older versions of Python

CPython is mot the only Python. Portability is an issue also.

Re: Python dicts are now ordered

#44
post #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…

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

Dicts are ordered, specifically insertion-ordered, not sorted.

Sometimes they'll be sorted:

  D = {i:i for i in range(10)}
... specifically, when you insert them in sorted order. But then you can break the sortedness on your next insert:

  D[-1] = -1
What it allows is parallel iteration:

  zip(D.keys(), D.values())
now being synonymous with

  D.items()
This enables, nay, encourages people to write code that is very subtly broken in 3.5 and below.

Re: Python dicts are now ordered

#45
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.

PHP has had this undocumented feature since forever and so did all mainstream Javascript engines.

Big whoop

Re: Python dicts are now ordered

#46
post #39
post #36

Earlier quoted context omitted.

You're right, I read the gp too quickly. But in the case of downgrading, I'm fairly sure there's a number of other breaking changes that can't trivially downgrade minor versions. Like f-strings were only introduced in python3.6 as I recall. Async keyword only exists as of 3.4 as well I think?

Introducing things is different than changing things.

[deleted]

Re: Python dicts are now ordered

#47
post #20

Earlier quoted context omitted.

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…

It doesn’t break existing code. Code written for Python 3.7 might break on older versions of Python

If you know you're supporting old code, use OrderedDict.

you arguably ought to anyway, for explicitness.

Re: Python dicts are now ordered

#48
post #10

This is a solid argument for why you should have more datatypes available to you. If your program relies on an insertion order, say so. If anyone ever moves to another implementation, they will thank you for it.

It's extra annoying because collections.OrderedDict has been in the standard library for ages.

Re: Python dicts are now ordered

#49
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.

Actualy, dict are already ordered since a long time(since python 3 I think), but it wasn't certified. It was just the result of a new implementation of dict structure.

I can't imagine how it can break something. In which case can you have an advantage to have an unordered list? Biggest downside can be about perf, but I don't think it's the case here.

Re: Python dicts are now ordered

#50
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.

It's not ambiguous, because it refers to the long pre-existing OrderedDict class.

Yes, I see that, but the title is unclear.
Post reply on HN