Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

281–290 of 457 posts

Re: Python dicts are now ordered

#281
post #267

Earlier quoted context omitted.

I mean youd probably have to iterate. Either that or use a package with proper array support.

That's messed up

In practice, it's almost never an issue. The standard lib has push/pop/shift/pad methods available.

So example, if you had numeric indices that you wanted to guarantee that index order is equal to insertion order, you'd create an array using $my_array = array_pad([], 20, null); If it's an array that you didn't create, the ksort(...) method will order the array for you as expected.

Edit: after reading through the documentation on array_pad, it looks like it will even correct key order in existing arrays. https://www.php.net/manual/en/function.array-pad.php#87735

Re: Python dicts are now ordered

#282
post #164

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…

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

The ordering property is a side effect of the new and more cpu/memory efficient data structure. It would be very surprising to say no to a 2x performance jump to avoid the (sometimes useful) ordering.

Re: Python dicts are now ordered

#283
post #125
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.

It's not breaking, it was implementation defined, now it's guaranteed order. If a new guarantee or behaviour were a breaking change, every non-patch release of a semversioned tool (which python isn't) would be major.

> It's not breaking, it was implementation defined

And randomised at interpreter start, since Python 3.3.

Re: Python dicts are now ordered

#284
post #251

Earlier quoted context omitted.

> about a feature we had since Python 3.6, in 2016 No, you had that feature in one implementation. Now it's in the language specification. That's vastly different because only now you can rely on it without fearing it can go away with the next release.

First, cPython is 99% of Python deployments. So much that if a script works on it but not on another implementation, people often consider the later broken. As this post proves that most people don't even know about this feature, you can be pretty sure the vast majority of people don't know about pypy, micropython, etc. Secondly, even if you want to nit pick, Python 3.7 made it official more than one year ago. We are…

[deleted]

Re: Python dicts are now ordered

#285

Earlier quoted context omitted.

But you can also just create them with named arguments, like this: collections.OrderedDict(y = "first", x = "second")

This also wasn't guaranteed to work since kwargs is a dict under the hood.

FWIW they could have used a special-purpose dict guaranteeing order for that. I don't know if they ended up using the normal dicts but the proposal to keep kwarg ordering predates the new dicts, and the PEP even notes that it's unclear the new dicts would be merged.

Same with class attribute ordering, PEP 520 still talks about using OrderedDict, however it was only merged after the new dicts, and even if ordering was not defined at the time (as part of Python-the-language) PEP 520 could use this internal property just fine and the ordereddict-based version was stripped out, the PEP was essentially accepted as a no-op (the ordering properties would just be officially documented).

Re: Python dicts are now ordered

#286

Earlier quoted context omitted.

Point of order: Ruby’s hashes do preserve insertion order. Python dicts are now the same.

Good for Ruby! Nice to hear. I'm pretty sure they didn't in Ruby ~1.8 though. It's been awhile :-) Can't remember exactly which version, but it bit me more than once so I'm pretty sure :)

Yup, I think Ruby 1.9 or so made hashes ordered.

Re: Python dicts are now ordered

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

Code that assumes no order cannot be broken by a specific order unless it was setup to assume everything but that specific order in which case it was already broken, but only randomly showing it’s broken nature.

Re: Python dicts are now ordered

#288

This is a refreshing update for those of us that like having code which will always act in the same way across multiple invocations. Now, if only Lua could follow the same path with their “tables” (“tables” is what Lua programmers call their form of Python’s “dictionaries” and Perl’s “hashes”). I just spent eight hours earlier this week debugging Lua code which would run differently on different invocations of the sa…

You can write an iterator yourself, no need to leave ?pairs idiom:

  function sortpairs(t)
    local keys = { }
    for key in pairs(t) do
      table.insert(keys, key)
    end
    table.sort(keys, function (a, b)
      return tostring(a) 

Re: Python dicts are now ordered

#289
post #228

Earlier quoted context omitted.

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.

IIRC it used to just start iteration at a pseudorandom index and then iterate normally. I looked at it couple years ago, don't know if they changed it.

Re: Python dicts are now ordered

#290

Earlier quoted context omitted.

Issue seems to be silent incorrect behavior, what happens if you attempt to run python code containing f-strings using an older python version. Does it raise an exception? That's good! What happens now if you write code for 3.7 which takes advantage of the new ordering and someone grabs it from your repo and runs it using 3.2, it would happily give incorrect results and noone is the wiser.

If you expect this situation you can assert the language version.

But the whole point is that some developer won’t expect that someone would run their code on an older Python, isn’t it?
Post reply on HN