Python dicts are now ordered
221–230 of 457 posts
Re: Python dicts are now ordered
#222It 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.
The new dict does not come with a performance hit, let alone a significant one. It’s much more memory efficient and generally slightly faster.
https://apps.dtic.mil/dtic/tr/fulltext/u2/a627127.pdf
do you have any references to back your claim that its actually faster?
Re: Python dicts are now ordered
#223There's so many cases where it's a benefit for map entries to retain order (and none where it's a problem). PHP really got this one right (and immediately messed it up by mixing ordered maps with arrays into a big soup, but hey, PHP). And so did, JS, sorta-kinda-by-accident.¹
When I went from PHP to Ruby back in the day, Hashes not being ordered definitely was my biggest gotcha. Everything about Ruby (except the deployment) was nicer, but the hashes... I've spent serious amounts of extra thinking just to make code work that would've worked out of the box in PHP.
Yay for ordered maps! Every language should have them, they're the best! There's just so much ergonomics packed in such a simple API.
1) JS objects are ordered maps, iff the keys are strings that cannot be parsed as an integer (really) (yeah it's nuts) (but still better than unordered maps!!)
Re: Python dicts are now ordered
#224Earlier quoted context omitted.
The Go designers went the other way (as they often do): > When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next. Actually it is not only "not guaranteed to be the same", the runtime actively makes sure that the iteration order is actually different so you don't even start to rely on it...
That’s fairly common, as it is protection against denial-of-service attacks that use hash collisions to make code perform poorly ( https://events.ccc.de/2011/12/28/crypto-talk-at-28c3-effecti... ) If I read https://lwn.net/Articles/474912/ correctly, Perl fixed that in 2003. C#, Java and Swift do it, too. As does (or, reading this, did?) Python ( https://bugs.python.org/issue13703 )
Re: Python dicts are now ordered
#225I 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
#226This is awesome, because the ordered map is the best data structure out there for easy & predictable programming, possibly only barring the array. There's so many cases where it's a benefit for map entries to retain order (and none where it's a problem). PHP really got this one right (and immediately messed it up by mixing ordered maps with arrays into a big soup, but hey, PHP). And so did, JS, sorta-kinda-by-acciden…
No, it didnt. What PHP calls "array" is actually an Ordered Map, as you alluded to:
it literally says that in the documentation, paragraph one, sentence one:
> An array in PHP is actually an ordered map.
The issue, if one exists, is that PHP doesnt have a sequence type:
but one is provided as a package:
Re: Python dicts are now ordered
#227This post is massively popular despite talking about a feature we had since Python 3.6, in 2016, that was posted on HN at the time and that is featured in most popular tutorials.
A good reminder that most of the world doesn't revolve about my favorite language. And that information is not that fast to spread.
Re: Python dicts are now ordered
#228Earlier quoted context omitted.
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
#229Earlier quoted context omitted.
right, so making it implicit is bad design
It's part of the zen of Python: Explicit is better than implicit.