Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

221–230 of 457 posts

Re: Python dicts are now ordered

#222
post #218

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.

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.

pretty strong evidence to the contrary

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

#223
This 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-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

#224
post #84

Earlier 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 )

The hash value order and insertion order are unrelated.

Re: Python dicts are now ordered

#225

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.

Many apps bundle Python along with their app, either within a container image or as a complete portable copy distributed with the binary. We've been building FROM python:3.8 for a few months.

Re: Python dicts are now ordered

#226

This 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…

> PHP really got this one right (and immediately messed it up by mixing it with arrays, but hey, PHP).

No, it didnt. What PHP calls "array" is actually an Ordered Map, as you alluded to:

https://yaml.org/type/omap

it literally says that in the documentation, paragraph one, sentence one:

> An array in PHP is actually an ordered map.

https://php.net/types.array

The issue, if one exists, is that PHP doesnt have a sequence type:

https://yaml.org/type/seq

but one is provided as a package:

https://php.net/class.ds-sequence

https://github.com/php-ds/extension

Re: Python dicts are now ordered

#227
I live in the Python bubble, so I haven't realized until this post that so few people knew about that.

This 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

#228

Earlier 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.

No, it isn't random enough for that.

Re: Python dicts are now ordered

#229
post #78
post #52

Earlier quoted context omitted.

right, so making it implicit is bad design

It's part of the zen of Python: Explicit is better than implicit.

Python is nothing like its guiding principles. That's why it's Zen -- the principles are a collection of contradictory statements, given what you will encounter in real world Python. You're meant to be confused and meditate on it.
Post reply on HN