Hash Map in Python (original) (raw)

Last Updated : 25 Oct, 2025

A hash map is a data structure that stores key-value pairs and allows fast access, insertion and deletion of values using keys. Python comes with built-in hash maps called dictionaries (dict). Keys are required to be unique and immutable (such as strings, numbers, or tuples), while values can be any Python object.

hashmap

HashMap Basics

Applications

How Hash Maps Work

Hash Function:

Collision Handling:

Key Operations

Python Implementation

Below is the implmentation for Hash Map from scratch:

1. Class Creation

Python `

class HashTable: def init(self, size): self.size = size self.hash_table = [[] for _ in range(size)]

`

2. Insert or Update (set_val)

Python `

def set_val(self, key, val): hashed_key = hash(key) % self.size bucket = self.hash_table[hashed_key]

for index, (record_key, _) in enumerate(bucket):
    if record_key == key:
        bucket[index] = (key, val)
        return
bucket.append((key, val))

`

3. Retrieve (get_val)

Python `

def get_val(self, key): hashed_key = hash(key) % self.size bucket = self.hash_table[hashed_key]

for record_key, record_val in bucket:
    if record_key == key:
        return record_val
return "No record found"

`

4. Delete (delete_val)

Python `

def delete_val(self, key): hashed_key = hash(key) % self.size bucket = self.hash_table[hashed_key]

for index, (record_key, _) in enumerate(bucket):
    if record_key == key:
        bucket.pop(index)
        return

`

5. Display (__str__)

Python `

def str(self): return "".join(str(bucket) for bucket in self.hash_table)

`

6. Creating and Printing the Hash Table

Python `

ht = HashTable(3) print(ht)

`

[][][]

Complete Code

Python `

class HashTable: def init(self, size): self.size = size self.hash_table = [[] for _ in range(size)]

def set_val(self, key, val):
    hashed_key = hash(key) % self.size
    bucket = self.hash_table[hashed_key]

    for index, (record_key, _) in enumerate(bucket):
        if record_key == key:
            bucket[index] = (key, val)
            return
    bucket.append((key, val))

def get_val(self, key):
    hashed_key = hash(key) % self.size
    bucket = self.hash_table[hashed_key]

    for record_key, record_val in bucket:
        if record_key == key:
            return record_val
    return "No record found"

def delete_val(self, key):
    hashed_key = hash(key) % self.size
    bucket = self.hash_table[hashed_key]

    for index, (record_key, _) in enumerate(bucket):
        if record_key == key:
            bucket.pop(index)
            return

def __str__(self):
    return "".join(str(bucket) for bucket in self.hash_table)
    

ht = HashTable(3) print(ht)

`

**Example: Let's create a hashmap and perform different operations to check if our class is working correctly.

Python `

ht = HashTable(3)

ht.set_val('apple', 10) ht.set_val('banana', 20) ht.set_val('cherry', 30)

print("Hash Table:", ht)

print("Value for 'banana':", ht.get_val('banana')) print("Value for 'apple':", ht.get_val('apple'))

ht.set_val('apple', 50) print("Updated Hash Table:", ht)

ht.delete_val('banana') print("After Deletion:", ht)

print("Value for 'banana':", ht.get_val('banana'))

`

**Output

Hash Table: [('cherry', 30)][('apple', 10)][('banana', 20)]
Value for 'banana': 20
Value for 'apple': 10
Updated Hash Table: [('cherry', 30)][('apple', 50)][('banana', 20)]
After Deletion: [('cherry', 30)][('apple', 50)][]
Value for 'banana': No record found

**Explanation:

Advantages and Disadvantages of Hash Maps

**Advantages **Disadvantages
Fast key-based access (average **O(1) time complexity) Collisions can slow down operations
Supports negative and non-integer keys Large key sets may cause frequent collisions
Maintains insertion order and allows efficient iteration Inefficient performance with poorly designed hash functions