![]() ![]() I examined it for 3 Operations: Lookups, Insertion and Traversals. Hey very interesting post and good to know, but I used your example with timers to verify the claims about the similar insertion/traversal times. Turns out, they have their own implementation. Until this moment I thought of sets as basically thin wrappers around actual dicts operating on their keys and neglecting values. Frankly, I can't see any point in trying to make it work :-)Īlso, dicts are bigger, by an order of magnitude: In : getsizeof() ![]() You'd have to always mention indexes explicitly when working with a dict, there's no such thing as. listĭoes it mean that a dict with int keys is the same as list? There's still a few practical differences. I don't know the exact politics of how this useful property progressed from an implementation detail to a guaranteed behavior. Since the entries array is populated sequentially, it naturally ensures the order.Īs far as I know the initial reason for this change was saving space by sharing hash tables of multiple dicts with the same set of keys (which in Python means instances of the same class, for example). ![]() Since version 3.6 CPython holds keys and values in a separate dense array, while the hash table itself only holds indexes into that: It is also sparse, with unoccupied holes in a pre-allocated array: That's it :-)Ī plain hash table holds both keys and values in a pseudo random order determined by hashes calculated from keys. So if you want to discuss fundamental differences you can pretty much only point out that dict values are accessible by keys, which could be of any immutable type, while list values are indexed with integers. This behavior was an implementation detail of CPython from 3.6. Quoting the docs referenced above:Ĭhanged in version 3.7: Dictionary order is guaranteed to be insertion order. There were several moments over the last few weeks when I heard people discuss differences between Python lists and dicts and one of the first ones mentioned was that lists are ordered and dicts are not. Dicts are now ordered, get used to it Ivan Sagalaev, ![]()
0 Comments
Leave a Reply. |