# CS102 - Hash Maps - Blossom Project

Hello, I am newbie, first time leaner here. I have questions for this exercise and hope they’re not too silly.

This is the final code:

``````from linked_list import Node, LinkedList
class HashMap:
def __init__(self,size):
self.array_size = size
self.array = [LinkedList() for i in range(size)]
def hash(self,key):
hash_code = sum(key.encode())
return hash_code
def compress(self,hash_code):
return hash_code % self.array_size

def assign(self,key,value):
array_index = self.compress(self.hash(key))
list_at_array = self.array[array_index]
for item in list_at_array:
if key == item[0]:
item[1] = value
return

def retrieve(self,key):
array_index = self.compress(self.hash(key))
list_at_index = self.array[array_index]
for item in list_at_index:
if item[0] == key:
return item[1]
else:
return None
``````

So first, since each index of the array is a Linked List, how is it possible to iterate through the Linked List by this?

``````    list_at_array = self.array[array_index]
for item in list_at_array:
``````

And secondly, why don’t we insert the new [key,value] directly to self.array[array_index] but instead insert them through an instance variable list_at_array. Doesn’t it only assign the new [key,value] to the instance variable, and not the array index it self?
I mean the code works perfectly fine. But I just don’t get it.

``````  def assign(self,key,value):
array_index = self.compress(self.hash(key))
list_at_array = self.array[array_index]
for item in list_at_array:
if key == item[0]:
item[1] = value
return
``````

I don’t remember if they talked about this when teaching LinkedList, but there’s code in linked_list.py that allows iteration through the list. The folder icon lets you open the other files available for the lesson.

``````  def __iter__(self):
while(current_node):
yield current_node.get_value()
current_node = current_node.get_next_node()
``````

I think it’s using the variable to make it easier to follow, this seems to work:

``````  def assign(self, key, value):
array_index = self.compress(self.hash(key))
#list_at_array = self.array[array_index]
for i in self.array[array_index]:
if key == i[0]:
i[1] = value
return
``````

Thanks a lot @bavarcarus .
It makes more sense now with the iter method.
And this one also seems to be better to understand to me:

``````  def assign(self, key, value):
array_index = self.compress(self.hash(key))
if you had a variable that was named `this_is_way_too_long_but_I_cannot_change_it`
you could just say `stupid_name = this_is_way_too_long_but_I_cannot_change_it`