Python Frequently Asked Questions, Release 3.9.5 43 3.18. How are lists implemented in CPython? • Mark lists as read-only once they are used as a dictionary key. The problem is that it’s not just the top-level object that could change its value; you could use a tuple containing a list as a key. Entering anything as a key into a dictionary would require marking all objects reachable from there as read-only – and again, self-referential objects could cause an infinite loop. • Allow lists as keys but tell the user not to modify them. This would allow a class of hard-to-track bugs in pro- grams when you forgot or modified a list by accident. It also invalidates an important invariant of dictionaries: every value in$_{d.keys()}$ is usable as a key of the dictionary. • Make a copy when using a list as a key. This doesn’t work because the list, being a mutable object, could contain a reference to itself, and then the copying code would run into an infinite loop. would raise a$_{KeyError}$ exception because the id of the$_{[1, 2]}$ used in the second line differs from that in the first line. In other words, dictionary keys should be compared using$_{==}$ , not using$_{is}$ . mydict ={[ 1,2]:'12 '} print(mydict[[1,2]]) Some unacceptable solutions that have been proposed: If you want a dictionary indexed with a list, simply convert the list to a tuple first; the function$_{tuple(L)}$ creates a tuple with the same entries as the list$_{L}$ . Tuples are immutable and can therefore be used as dictionary keys. The hash table implementation of dictionaries uses a hash value calculated from the key value to find the key. If the key were a mutable object, its value could change, and thus its hash could also change. But since whoever changes the key object can’t tell that it was being used as a dictionary key, it can’t move the entry around in the dictionary. Then, when you try to look up the same object in the dictionary it won’t be found because its hash value is different. If you tried to look up the old value it wouldn’t be found either, because the value of the object found in that hash bin would be different. 3.20 Why must dictionary keys be immutable? 3.19 How are dictionaries implemented in CPython? 3.18 How are lists implemented in CPython? CPython’s lists are really variable-length arrays, not Lisp-style linked lists. The implementation uses a contiguous array of references to other objects, and keeps a pointer to this array and the array’s length in a list head structure. This makes indexing a list a[i] an operation whose cost is independent of the size of the list or the value of the index. When items are appended or inserted, the array of references is resized. Some cleverness is applied to improve the performance of appending items repeatedly; when the array must be grown, some extra space is allocated so the next few times don’t require an actual resize. CPython’s dictionaries are implemented as resizable hash tables. Compared to B-trees, this gives better performance for lookup (the most common operation by far) under most circumstances, and the implementation is simpler. Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key and a per-process seed; for example, “Python” could hash to - 539294296 while “python”, a string that differs by a single bit, could hash to 1142331976. The hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you’re storing keys that all have different hash values, this means that dictionaries take constant time – O(1), in Big-O notation – to retrieve a key. • Hash lists by their address (object ID). This doesn’t work because if you construct a new list with the same value it won’t be found; e.g.: