How are dictionaries implemented?
Python’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 built-in hash function. The hash code varies widely depending on the key; for example, “Python” hashes to -539294296 while “python”, a string that differs by a single bit, hashes 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 computer science notation — to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the internal array as the dict.keys and dict.items methods do will output the dictionary’s content in some arbitrary jumbled order.