5 ms·
Dicts now retain order now (in other words all dicts are ordered dicts). I support this, but I also guarantee this is going to bite me in the ass at least once
by b_tterc_p 8y ago
Dicts now retain order now (in other words all dicts are ordered dicts). I support this, but I also guarantee this is going to bite me in the ass at least once sharing notebooks with someone on an older version of python as I expect to use this liberally. Ah well.
- bratao 8y agoJust to clarify. This behavior is expected since 3.6: As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python. As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature. [1] https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6 https://stackoverflow.com/questions/39980323/are-dictionarie...
- blattimwind 8y agoCPython implementation detail vs. Python language feature remains a relatively meaningless distinction.
- anticensor 8y agoCPython is not the only Python interpreter out there.
- JeremyBanks 8y agoThe documentation sometimes suggests otherwise, but since there is no Python language specification, declaring something "part of the language" seems to have no meaning if it's not "implemented in CPython". PyPy has had ordered dicts for longer than CPython, and Jython and IronPython don't look like they're ever going to support Python 3, so I'm not sure who this declaration is even even hypothetically relevant to.
- richardwhiuk 8y agoIt's relevant in the fact that you shouldn't rely on it as a programmer prior to this release. Changing it would have been considered a none breaking change.
- JeremyBanks 8y agoGood point.
- earenndil 8y agoThe other major python implementation, pypy, effectively implements CPython. (And insofar as it doens't, it's heading that way.)
- setr 8y agoEven if you stick purely to CPython, it still matters in that there's no guarantee that it'll still be ordered when you update (if you were reading 3.6 dicts). It's not just an implementation detail for CPython, but an implementation detail for specifically CPython 3.6 (and any other version maintaining that status). So now that its upgraded to language feature, you can actually expect backwards compatibility in future updates.
- xapata 8y agoIt's a surprisingly useful feature.
- GeorgeTirebiter 8y agoHi, might you be able to give a short example? Thanks!
- jsmeaton 8y agoForms (and models) in Django are one such thing. When you define fields on a form at the class level, Django maintains a global counter so the fields can retain their order with respect to each other. Now, with ordered dictionaries, that order is maintained for free. Kwarg order in functions is also maintained as far as I know.
- b_tterc_p 8y agoOne thing I do frequently is create an ordered dict where the keys are either regex patterns or lambda functions that return true or false; and the values are some sort of label (let’s say some sort of document type). Then I’ll create a function that will take that ordered dict plus some data, and return the first label that the data conforms to (e.g. regex pattern matches some text input) This is a handy way of making classification rules for a many stepped process, where certain rules have priority over others. The alternative would be a lengthy set of ifs and elifs which is both ugly and not fun to edit on the fly.
- amelius 8y ago> Dicts now retain order now (in other words all dicts are ordered dicts). I think you can be more clear here: all dicts are ordered by insertion order (not by key).
- b_tterc_p 8y agoFair. I was referring to the precious data structure for this use case, collections.OrderedDict but that’s not self evident if you’re not familiar.