Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

201–210 of 457 posts

Re: Python dicts are now ordered

#201
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 think if you are a library developer targeting different versions of Python, you generally know what features you can and can't use from new versions of Python (ie not many), and ideally you are also testing with each version in CI.

Re: Python dicts are now ordered

#202

This is an amazing contribution to the language. A mixture of speed and convenience, probably made by volunteers. As for people criticizing a change to what use to be a non-deterministic ordering of a dict iteration; I don't know what to say to them, other than, are you serious? There are people out there who are working for us, they work for free and they did some heavy lifting to give us this. They might read what…

I was under the impression python dict ordering was deterministic, just not in an order recognisable by humans, i.e. ordered by hashes. Was this not the case?

Re: Python dicts are now ordered

#203

I'm glad to see other languages finally catching up to PHP. I'm joking (kind of) but after a lot of years of doing this, I've begun de-prioritizing pure abstractions and favoring the way that humans tend to do things on their own. Technically this is along the lines of the worse-is-better philosophy. The single biggest cost in software development is friction. Performance, size, etc are all less important, because th…

For those that don't know PHP associated arrays are equivalent pythons dictionaries. They are implemented as an ordered map.

https://www.php.net/manual/en/language.types.array.php

Its pretty much the main data type used in php. Once you get used to it is pretty powerful for dynamic languages. They even have built in sorts:

https://www.php.net/manual/en/array.sorting.php

They are "simple arrays" that have numeric indexes, but I find those used less frequently. (They can morph into the key->value pair).

Re: Python dicts are now ordered

#204
post #164

Earlier quoted context omitted.

Other languages don't generally have special order guarantees about standard maps. This seems very idiosyncratic.

Golang map iteration is returned in random order specifically to make sure that people don't rely on the order. I think this feature says a lot about the philosophy of Python vs Go.

Sounds like a fast and idiomatic way to shuffle a deck of cards is then to convert to a map and back.

Re: Python dicts are now ordered

#205
I think this change is great, but this really only becomes news again once all major LTS are shipping with at least Python 3.7, right? No one can really use it in code they plan to distribute at the moment. Maybe I am underestimating the amount of Python code that is meant for internal or personal use only.

Re: Python dicts are now ordered

#206
post #202

This is an amazing contribution to the language. A mixture of speed and convenience, probably made by volunteers. As for people criticizing a change to what use to be a non-deterministic ordering of a dict iteration; I don't know what to say to them, other than, are you serious? There are people out there who are working for us, they work for free and they did some heavy lifting to give us this. They might read what…

I was under the impression python dict ordering was deterministic, just not in an order recognisable by humans, i.e. ordered by hashes. Was this not the case?

From the docs: "CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions."

So I would claim that equates to non-deterministic

Re: Python dicts are now ordered

#207

Earlier quoted context omitted.

> As for people criticizing a change to what use to be a non-deterministic ordering of a dict iteration; I don't know what to say to them, other than, are you serious? There are people out there who are working for us, they work for free and they did some heavy lifting to give us this. They might read what you wrote and think, "Why bother? Maybe I should spend my weekends playing with my kids instead." I don't agree…

As it’s widely known, more often than not “criticism” of open source software quickly devolves into hate and toxicity. Helpful criticism is great but be careful with defending the “criticism culture” around foss, it’s often angry unhappy people that want it all for free on a golden platter, and yesterday.

That does not matter, nothing is above criticism.

Re: Python dicts are now ordered

#208
post #202

Earlier quoted context omitted.

I was under the impression python dict ordering was deterministic, just not in an order recognisable by humans, i.e. ordered by hashes. Was this not the case?

From the docs: "CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions." So I would claim that equates to non-deterministic

I’d call undependable but maybe it’s the same thing in practice?

Re: Python dicts are now ordered

#209
post #44

Earlier quoted context omitted.

> 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: zi…

The property you mention at the end has been true of dicts since long before 3.5, at least since 2.7 if not forever. See https://docs.python.org/2/library/stdtypes.html#dict.items : > If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pair…

Sorry, yes, I was hasty. Ordered dicts supports such a thing with intervening modifications:

  oldkeys = list(D.keys())
  D[z] = foo(z)
now if you take

  zip(oldkeys, D.values())
then you're guaranteed to iterate over oldkeys, with the proper values associated with those keys -- if z was an oldkey, its value got updated; otherwise, it comes after oldkeys and gets dropped out of zip.

The subtlety of this is what I, and perhaps others, find the most jarring.

Re: Python dicts are now ordered

#210
It seems people dont realize that you can have your cake and eat it too. You can have both orderered and unordered maps in the same language:

https://yaml.org/type/map

https://yaml.org/type/omap

No reason to argue about which one to make "dict". In fact it would be better to have both because youre taking a performance hit (a significant one) by ordering the entries.

Post reply on HN