Skip to Content
Hash Maps: Python
Open Addressing in the Getter

With everything in our setter taken care of, we want to make sure that when we retrieve our value we’re retrieving the correct value.



In .retrieve() if possible_return_value has a different key than the one we’re looking for, we should continue searching.

Define a new variable called retrieval_collisions and set it equal to 1.


Insert a new while loop that checks if

possible_return_value[0] != key

In the while loop, we want to replicate our retrieval logic while increasing the count of retrieval_collisions so that we continue to look at other locations within our array.

Call .hash() with both the key and retrieval_collisions. Save that result into new_hash_code.


Plug new_hash_code into .compressor(). Save that result into retrieving_array_index.


Check self.array at retrieving_array_index and save the result as possible_return_value. Check against the three possibilities:

  • If it’s None, return None
  • If it has a value, but a different key, increment retrieval_collisions.
  • If it’s key matches our key return possible_return_value[1].
Folder Icon

Sign up to start coding

Already have an account?