FAQ: Hash Maps: Python - Handling Collisions in the Getter

This community-built FAQ covers the “Handling Collisions in the Getter” exercise from the lesson “Hash Maps: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Handling Collisions in the Getter

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

If so, return possible_return_value[1] , can somebody please explain this? Why did we choose the next one?

1 Like

Hi @byteninja86485,

In the exercise, Handling Collisions in the Setter, we arranged to store a key and the associated value together in a list. The first item in that list is the key, and the second item is the value. Accordingly, the index of the value in that list is 1.

In the current exercise, Handling Collisions in the Getter, we want to return the value associated with a specified key. In the retrieve method, we have this:

    possible_return_value = self.array[array_index]

If there is a key, value pair in possible_return_value, it would be in the list that we stored there. We check whether the key is the one we are seeking. That key would be at index 0 of the list. If we have found the key we are seeking, we return the value, which is at index 1.

Hence, we have this:

      if possible_return_value[0] == key:
        return possible_return_value[1]
2 Likes

Hi! I understood all about the code and explanation until I come across with this code block. Could anyone help me please?

  def assign(self, key, value):
    array_index = self.compressor(self.hash(key))
    current_array_value = self.array[array_index]

    if current_array_value is None:
      self.array[array_index] = [key, value]
      return

    if current_array_value[0] == key:
      self.array[array_index] = [key, value]
      return

So in the code above, to assign a key and value in a Hashmap and you check if there is current_array_value.

But I don’t understand why we check if current_array_value[0](which is key) is the same key as the given argument(key). If the key is the same key, why do you try to save the same thing again?

or is it because to reset the value that was already saved with the key to a new value under the same key?

I hope you could understand my question. :flushed:

Yes, if key is already there, we need to associate value with it.

1 Like

Hi ,

if current_array_value is None:

  self.array[array_index] = [key, value]

  return
  1. I dont’ understand the code structure "if current_array_value is None: " why uses “is” , not "== "

  2. why return left vacant ?

Thank you for clarify

It’s possible that you can find objects with unusual equality attributes such that object == None evaluates to True even if they are not the None object. It’s unlikely but for None most guidance suggests using is and is not identity operations rather than equality testing. Have a web search for the same if you want more info.

The return isn’t really needed here but some style guides prefer having a return even if it returns nothing. It doesn’t really matter.

1 Like