Hash table tutorial in Python


Understanding various complex data structures will help in learning more about programming in python. The hash table is an important data structure to focus on. It is easy to learn and implement.

Hash table tutorial in Python

What is a hash table?

The Hash table is a data structure that is used for implementing a map. It can map a value pair with a key to store and access elements quickly. Mapping can be efficient when an efficient hash function will be applied. Hast tables make it easy to access, remove, insert data. The purpose of every hash function h is to map a key k to an integer in the range of [0, N-1]. Here, N refers to the capacity of the bucket array for a hash table. Hash function value h(k) is used as the index in our bucket array.

When a hash function maps each item to a unique slot then it is known as a perfect hash function. You can have a perfect hash function by increasing the size of table. Moreover, another thing to know is if two or more keys exist with the same value then two different items are mapped to the same bucket. This is known as collision.

There are slots that contain various positions where each position holds an element and is named by an integer value beginning from zero. This way, we can have a slot 1, slot 2, slot 3, and so on. As hash table contains no items in the beginning so a slot remains empty at that time. In python, the implementation of a hash table can be observed in the dictionary data type. Some important things to note down are-

  • The dictionary keys can be generated with the help of hashing function that provides unique results for every unique value given to the hash function
  • A dictionary has no ordering of data elements
  • The first hash function takes an item and then it is divided by the table size. It provides the remainder in the form of the hash value (h(item)= item%11). Let’s study some of the implementation of a hash table in the dictionary.

Accessing values

You can access a dictionary values as represented in the following code-

dict = {'Name': 'Aniket', 'Age': 22, 'Qualification': 'Graduate'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age']


dict['Name']: Aniket dict['Age']: 22

Modifying dictionary

You can tweak a dictionary by adding a new key-value pair, deleting an entry or adding a new one, edit the elements of it.

dict = {'Name': 'Aniket', 'Age': 22, 'Qualification': 'Graduate'} dict['Age'] = 23; dict['College'] = "Stanford University"; print "dict['Age']: ", dict['Age'] print "dict['College']: ", dict['College']


dict['Age']: 23 dict['School']: Stanford University

Deleting Elements

It’s up to you if you want to delete the entire elements or only single elements.

<div class="codecss"><xmp> dict = {'Name': 'Aniket', 'Age': 22, 'Qualification': 'Graduation'} del dict['Name']; dict.clear(); del dict ; print "dict['Age']: ", dict['Age'] print "dict['Qualification']: ", dict['Qualification']


dict['Age']: Traceback (most recent call last): File "test.py", line 8, in print "dict['Age']: ", dict['Age']; TypeError: 'type' object is unsubscriptable

The above code after execution is providing error and an exception is raised. It is only because of that after del dict. So, these were few of the implementations of the hash table in python dictionary. Practicing as much as you want will make you a better programmer.




Hot Deals ends in

Technical Quizzes Specially For You:


Search Tags

    Python hash table example