Python: Hashmap using Lists


Video is ready, Click Here to View ×

How to implement a hashmap (AKA dictionary, hash table, associative array) in Python using lists / arrays. Explains what a hash map is, how it works, and how to code it in Python with add, delete, and get functions.
See my latest code on GitHub here,

28 thoughts on “Python: Hashmap using Lists

  1. Hey ! If your'e in an interview , can there be any circumstance (i.e coding problems) where you could prefer any other language over Python? Simply put, what are the downsides or 'not a perfect language to do this task ' kindof things with python? Because i have been using c++ till now and i find python to be simpler and clean!

  2. So in the add function by just simply updating the value are you purposely avoiding collisions by just updating the value when the user passes in a key that already exists in the bucket because what if the user wanted to add another pair at that key and not overwrite the old value?

  3. The vedio is amazing…kudos. But Have I doubt. Key is immutable in python..If I am not wrong , here we can modify the key…Can we use tuple for keys and list for values?..

  4. Dear Joe,
    I am preparing for a coding interview and got stuck with this question for many days.

    "Given the functions Up(), Down(), Left(), Right(), Enter () write the function that would take Str (like Hobbit:_Unexpected_Journey) and type it using keypad (the kind you have on Smart TVs, etc.)" in Python.

    Could you please be so kind to help me? Thank you so much.


  5. class HashBoi:
        def _init_(self):
            self.size = 6
   = [None] * self.size    def _get_hash(self, key):
            hash = 0
            for char in str(key):
                hash += ord(char)
            return hash % self.size    def add(self, key, value):
            key_hash = self._get_hash(key)
            key_value = [key, value]        if[key_hash] is None:
      [key_hash] = list([key_value])
                return True
                for pair in[key_hash]:
                    if pair[0] == key:
                        pair[1] = value
                    return True    def get(self, key):
            key_hash = self._get_hash(key)
            if[key_hash] is not None:
                for pair in[key_hash]:
                    if pair[0] == key:
                        return pair[1]
            return None
        def delete(self,key):
            key_hash = self._get_hash(key)        if[key_hash] is None:
                return False
            for i in range (0, len([key_hash])):
                if[key_hash][i][0] == key:
                    return True    def print(self):
            for item in
                if item is not None:
                    print(str(item))h = HashBoi()
    h.add('Bob', '567-8888')
    h.add('Ming', '239-6753')
    h.add('Ming', '333-8233')
    h.add('Ankit', '293-8625')
    h.add('Aditya', '852-6551')
    h.add('Alicia', '632-4123')
    h.add('Mike', '567-2188')
    h.add('Aditya', '777-8888')
    print('Ming: ' + h.get('Ming'))

  6. Quick question: at 8:20 of the video. In function "delete", one scenario is missing. If[key_hash] is not empty, and no pair in[key_hash] list match our key. In this case, the delete function will not return anything.

  7. Here i could not understand something related to the output like you have taken size of the array as 6. Then for adding BOB its ASCII vlaue is 66+79+66 = 211 thus 211%6 =1 so why BOB is added at a different index?

Leave a Reply

Your email address will not be published. Required fields are marked *