Home » Data Structures & Algorithms, Python29 January 2018

Hash Table implementation in Python [Data Structures & Algorithms]

This article deals with implementing Hash Table using Python programming language. Hash Table is a data structure where data are stored in an associative manner (in key, value format). The key/index is unique. This kind of storage makes it easier to find the data later on.

Hash Table stores data into an array format. It uses a hashing function that generates a slot or an index to store/insert any element or value.

We will be creating our own hashing function and hash table. But before that, let’s look into the built-in Python dictionary and Python list.

Python Dictionary

– This is a Python data type which is same as what is called “associative array” in other programming languages.
– Data are stored in key-value pairs.
– It’s easy to retrieve data from Python Dictionary.


Python List

– A list is another data type in Python.
– Data can be stored as tuples.
– It’s not as easy as python dictionary to retrieve items.
– We need to use a loop to search for any item in the list.
– It’s time-consuming to retrieve data.


Very Simple Implementation

Here is a very simple implementation of the hash table and hashing function.

Hash Function / Hashing

This deals with generating slot or index to any “key” value. Perfect hashing or perfect hash function is the one which assigns a unique slot for every key value. Sometimes, there can be cases where the hash function generates the same index for multiple key values. The size of the hash table can be increased to improve the perfection of the hash function.

First of all, let’s create a hash table of size 10 with empty data.

Below is a simple hash function that returns the modulus of the length of the hash table. In our case, the length of the hash table is 10.

Modulo operator (%) is used in the hashing function. The % (modulo) operator yields the remainder from the division of the first argument by the second.

Inserting Data into Hash Table

Here’s a simple implementation of inserting data/values into the hash table. We first use the hashing function to generate a slot/index and store the given value into that slot.


A collision occurs when two items/values get the same slot/index, i.e. the hashing function generates same slot number for multiple items. If proper collision resolution steps are not taken then the previous item in the slot will be replaced by the new item whenever the collision occurs.


In the example code above, we have inserted items Nepal and USA with key 10 and 25 respectively. If we try to insert a new item with key 20 then the collision occurs because our hashing function will generate slot 0 for key 20. But, slot 0 in the hash table has already been assigned to item ‘Nepal’.

As you can see, ‘Nepal’ is replaced by ‘India’ as the first item of the hash table because the result of hashing_func for keys 10 and 20 is the same (i.e. 0).

Collision Resolution

There are generally two ways to resolve a collision:

1. Linear Probing
2. Chaining

1. Linear Probing

One way to resolve collision is to find another open slot whenever there is a collision and store the item in that open slot. The search for open slot starts from the slot where the collision happened. It moves sequentially through the slots until an empty slot is encountered. The movement is in a circular fashion. It can move to the first slot while searching for an empty slot. Hence, covering the entire hash table. This kind of sequential search is called Linear Probing.

2. Chaining

The other way to resolve collision is Chaining. This allows multiple items exist in the same slot/index. This can create a chain/collection of items in a single slot. When the collision happens, the item is stored in the same slot using chaining mechanism.

While implementing Chaining in Python, we first create the hash table as a nested list (lists inside a list).

The hashing function will be the same as we have done in above example.

We change the insert function. We use append() function to insert key-value pairs in the hash table.

Standard Implementation

A more standard implementation of Hash Table with Python is presented below. We create three different functions to insert, search, and delete items from the hash table.

Python’s built-in “hash” function is used to create a hash value of any key. This function is useful as it creates an integer hash value for both string and integer key. The hash value for integer will be same as it is, i.e. hash(10) will be 10, hash(20) will be 20, and so on.

In the below code, note the difference in output while using 10 and ’10’. 10 (without quote) is treated as an integer and ’10’ (with quote) is treated as a string.

Create empty Hash Table

Creating the hash table as a nested list (lists inside a list).

Insert Data into the Hash Table

Let’s name each individual list inside the hash table list as ‘bucket’.

While inserting a new element into the hash table, we first search if the key already exists in the hash table.

– If the key is already present in the hash table, then we update its value with the new one.
– Otherwise, we insert a new key-value pair into the hash table.

Search Data from the Hash Table

While searching for any key in the hash table, we have to loop through each individual sublist.

Delete Data from the Hash Table

Deleting element (key-value pair) from the hash table is somewhat similar to inserting element. The loop is almost the same.

While deleting any existing element from the hash table, we first search if the key already exists in the hash table.

– If the key is present (found) in the hash table, then we simply delete it. We delete that particular key-value pair from the hash table.
– Otherwise, no operation is done. We can simply print a message saying that the key was not found in the hash table.


1. Hashing
2. A Simple Hash Table in Python

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed