Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python optimize dictionary lookups under the hood?

For example:

d = {"John": "Doe", "Paul": "Allen", "Bill": "Gates"}

Only imagine this had several thousand / million such names, all unique by first name.

If I wanted to see if the key "Paul" existed, what does it do under the hood?

like image 513
AureliusPhi Avatar asked Nov 17 '14 19:11

AureliusPhi


1 Answers

Python's dictionary implementation reduces the average complexity of dictionary lookups to O(1) by requiring that key objects provide a "hash" function. Such a hash function takes the information in a key object and uses it to produce an integer, called a hash value. This hash value is then used to determine which "bucket" this (key, value) pair should be placed into. Pseudocode for this lookup function might look something like:

def lookup(d, key):
    '''dictionary lookup is done in three steps:
       1. A hash value of the key is computed using a hash function.

       2. The hash value addresses a location in d.data which is
          supposed to be an array of "buckets" or "collision lists"
          which contain the (key,value) pairs.

       3. The collision list addressed by the hash value is searched
         sequentially until a pair is found with pair[0] == key. The
         return value of the lookup is then pair[1].
   '''
   h = hash(key)                  # step 1
   cl = d.data[h]                 # step 2
   for pair in cl:                # step 3
       if key == pair[0]:
           return pair[1]
   else:
       raise KeyError, "Key %s not found." % key

From the Python Wiki

like image 126
JNevens Avatar answered Nov 15 '22 06:11

JNevens