Live data from Hacker News

Python dicts are now ordered

softwaremaniacs.org

401–410 of 457 posts

Re: Python dicts are now ordered

#401
post #288

Earlier quoted context omitted.

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)

What Lua is lacking here (and why the above iterator function needs 17 lines) is the ability to have “for” go through a list ( without converting the list in to values returned by an iterator function), which would let us quickly and easily sort lists that “for” can use. Something like: d = {"foo": 2, "bar": 1, "zoo": 4} for k in sorted(d.keys()): print k (I’m not advocating Python here, since Perl has a similar way…

You can convert a list first and then feed it to a simple iterator. I don’t fully understand what your exact real-code issues can be, but hope this snippet may help:

  function vs(t)
    local i = 0
    return function (t)
      i = i + 1
      return t[i]
    end, t
  end

  function sorted(t, cmp)
    table.sort(t, cmp or function (a, b)
      return tostring(a) 
I.e. if “natively” means strictly “for in t” that generates values, then no, Lua can’t do that. But if “for in vs(t)” is okay, then that vs() is the solution.

Re: Python dicts are now ordered

#403
post #385
post #384

Earlier quoted context omitted.

You can use memcpy to remove items from the middle of a memory segment. It's probably not a single memory segment either; I imagine it works more like a Golang slice.

Memcpy is certainly O(N). But yeah, if they implement it with multiple slices (e.g. a B tree) it could be fast.

Hrm, I'm pretty sure I meant memmove: https://golang.org/src/runtime/slice.go

EDIT: I could have sworn they were shifting entire pages around without using a cycle per byte but I can't find any reference to that now haha.

Re: Python dicts are now ordered

#404

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.

I have lived in Python ~80% of my career (~50% now) currently), although a large chunk of my career was writing 2.7 compatible with 2.6 (thanks RHEL). For the last year and a half I have written 3.6+ exclusively and I didn't realize that regular dicts were now ordered. I don't use dicts if I require ordering, but if I did I would probably still reach for collections.OrderedDict... I wonder what the implications are o…

Dict ordering started from the need to have class attributes, args and kwargs ordered if I recall.

Re: Python dicts are now ordered

#405

Earlier quoted context omitted.

Came to say the same.. insertion order of dict has been here for years for 90+% of Python users (CPython implementation) and a part of the language spec also for almost as many years. I guess it’s good to spread awareness to HN readers who apparently were unaware, but the headline is very misleading.

90% seems optimistic. I don't think that many Python projects are running the most recent version in production. From personal experience, upgrading to 3.7 was especially a pain because of packages that used "async" for variable/kwarg names.

That’s a very good point, in that case even 50% would probably be optimistic.

We can probably attribute this article’s novelty to the slow adoption of Python 3. In that way it’s probably a good thing that it’s such a popular topic, even if it is old news.

Re: Python dicts are now ordered

#406
post #380

Wait, if it is held in a separate dense array, is removal of a key from a dictionary O(N)?

I _think_ this may be the implementation: https://github.com/python/cpython/blob/60ac6ed5579f6666130fc...

There are copious notes in there about the implementation and it indicates linked lists are used.

EDIT: Blargh, it's here: https://github.com/python/cpython/blob/60ac6ed5579f6666130fc... . That file references this blog post which indicates they may be flagging and repacking as dilap commented: https://morepypy.blogspot.com/2015/01/faster-more-memory-eff...

Re: Python dicts are now ordered

#408

Earlier quoted context omitted.

I have lived in Python ~80% of my career (~50% now) currently), although a large chunk of my career was writing 2.7 compatible with 2.6 (thanks RHEL). For the last year and a half I have written 3.6+ exclusively and I didn't realize that regular dicts were now ordered. I don't use dicts if I require ordering, but if I did I would probably still reach for collections.OrderedDict... I wonder what the implications are o…

Dict ordering started from the need to have class attributes, args and kwargs ordered if I recall.

No, that was a happy coincidence. The goal of the dict implementation was efficiency. Ordering was a side-effect.

Re: Python dicts are now ordered

#410

Earlier quoted context omitted.

In that situation, Matlab allocates an array of length 3, fills it with “empty” values (depending on the type), and then sets the third element to the value. That’s what I’d have expected to happen here too...

Would you expect it to do that if somebody wrote the following? $id = 12835151; $arr[$id] = Get_thing_with_id( $id );

yes.
Post reply on HN