![]() 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |