7 hours ago · Tech · hide

Due to dicts preserving the order of key insertion, they’re another example of objects that may have == True but with other methods yielding different values: >>> x = dict(a=1, b=2) >>> y = dict(b=2, a=1) >>> x == y True >>> list(x) ['a', 'b'] >>> list(y) ['b', 'a'] How does Python implement remembering key insertion order? Raymond Hettinger’s 2017 talk Modern Python Dictionaries A confluence of a dozen great ideas talks about how the ordering is a side effect of other optimizations. Slides. PEP 468 – Preserving the order of **kwargs in a function (2014) points to the message where Hettinger proposed compact dicts: [Python-Dev] More compact dictionaries with faster iteration. The ActiveState link therein is dead but archive.org has a copy: https://web.archive.org/web/20190217120809/http://code.activestate.com/recipes/578375/ From that working Python prototype, we can see the order getting preserved here: def __setitem__(self, key, value): hashvalue = hash(key) index, i =…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town