Python dict7/27/2023 The next section describes the collision resolution method used in the case of Python dictionaries. We could use a linked list to store the pairs having the same hash but it would increase the lookup time e.g. However, once we add the key ‘z’, there is a collision because it is not consecutive enough. We can see that the Python hash function does a good job when the keys are consecutive which is good because it is quite common to have this type of data to work with. The index for ‘b’ is 3, the index for ‘c’ is 2, the index for ‘z’ is 3 which is the same as ‘b’, here we have a collision. If the size of the array is 8, the index for ‘a’ will be: hash(‘a’) & 7 = 0. This means that having a simple computation makes sense in most of the cases. The probability to find an empty slot is high due to the resizing mechanism described below. This makes the computation of the slot index fast. If an array of size x is used to store the key/value pairs then we use a mask equal to x-1 to calculate the slot index of the pair in the array. Here we assume we are using a 64-bit machine. If you run hash(‘a’) in Python, it will execute string_hash() and return 12416037344. Set var x to (1000003 * x) xor value pointed by pĬache x as the hash so we don't need to calculate it again Set x to value pointed by p left shifted by 7 bits Initialize var p pointing to 1st char of string object The hash function for strings in Python is defined as: We are going to assume that we are using strings as keys for the rest of this post. Its most important hash functions (for strings and ints) are very regular in common cases: Python does not have this kind of hash function. A good hash function minimizes the number of collisions e.g. The goal of a hash function is to distribute the keys evenly in the array. It is an array whose indexes are obtained using a hash function on the keys. Python dictionaries are implemented using hash tables. The key ‘d’ does not exist so a KeyError exception is raised. Let’s add 3 key/value pairs to a dictionary: This post describes how dictionaries are implemented in the Python language.ĭictionaries are indexed by keys and they can be seen as associative arrays.
0 Comments
Leave a Reply. |