A Basic HashMap (Python) A hashmap is a data structure that implements an associative array abstract data type using keys and values and has a hash function to compute an index into an array, from which the desired value can be set and get [reference].



Based on some tutorial, I wrote this hashmap class:

class HashMap():
    def __init__(self):
        self.hahsmap_size = 32
        self.hashmap_data = [None] * self.hahsmap_size

    def __get_hash_mod_size(self, key):
        hash_key_var = hash(key+str(self.hahsmap_size*0.01))
        return hash_key_var % self.hahsmap_size

    def set_key_value(self, key, value):
        key_var = self.__get_hash_mod_size(key)
        key_value_list = [key, value]

        if self.hashmap_data[key_var] is None:
            self.hashmap_data[key_var] = list([key_value_list])
            return True
        else:
            for pair in self.hashmap_data[key_var]:
                print(pair)
                if pair[0] == key:
                    pair[1] = value
                    return True
            self.hashmap_data[key_var].append(key_value_list)
            return True

    def get_key(self, key):
        key_var = self.__get_hash_mod_size(key)
        if self.hashmap_data[key_var] is not None:
            for pair in self.hashmap_data[key_var]:
                if pair[0] == key:
                    return pair[1]
            return None

    def remove_key(self, key):
        key_var = self.__get_hash_mod_size(key)

        if self.hashmap_data[key_var] is not None:
            return False
        for i in range(len(self.hashmap_data[key_var])):
            if self.hashmap_data[key_var][i][0] == key:
                self.hashmap_data[key_var].pop(i)
                return True
    def print_hashmap(self):
        for item in self.hashmap_data:
            if item is not None:
                print(item)


hm = HashMap()
hm.set_key_value('A', '1')
hm.set_key_value('A', '2')
hm.set_key_value('B', '1')
hm.set_key_value('A', '3')
hm.set_key_value('A', '4')
hm.set_key_value('C', '1')
hm.set_key_value('D', '1')
hm.set_key_value('E', '1')
hm.set_key_value('E', '2')
hm.remove_key('A')
hm.remove_key('B')
hm.remove_key('B')
hm.print_hashmap()


If you had time, I'd appreciate a review.
The first thing that stands out here to me is that you should be using __get_item__ and __set_item__, and __repr__ instead of get-key, set-key-value and print_hashmap. This will opt you into the syntax of collections in python where you use d[key] to get a key, d[key] = val to set a value, and print(d) to print it. This may seem trivial, but the fact that python lets containers you have written to feel as nice as base ones is a large part of why Python is such a natural feeling language as opposed to something like Java, where this style would be considered correct.