Data Structures & Algorithms in Python – Introduction to Hash Tables

One of the fastest and efficient data structures are hash tables. These data structure uses mappings to associate keys with their corresponding values.

Hash tables map keys to their associated values through what is known as a hash function.

A hash function is a special function that maps the key to its value by transforming the key into a number. Then, the hash table uses the number to locate the corresponding value of that particular key.

Hash tables
Fig: Demonstration of a hash table.

Collisions

Often times the hash function will assign two different keys, each mapping to the same slot in the table. As a result, a collision takes place.

I am going to keep things simple on this post, as collision resolution is fairly a complex topic when it comes to hash tables.

However, just to give you a glimpse. One of the ways to resolve collision in hash tables is by implementing a method called open addressing.

This is a method through which you resolve the collision by inserting an item somewhere other than the desired address in the table.

Hash tables
Fig: Open addressing in a hash table.

Benefits & Time Complexity

Hash tables are probably one of the most efficient data structures in the programming world.

One of their main benefits is the ability to uniquely identify and map a given key to its corresponding value.

Moreover, they are very fast compared to other data structures. They provide a constant time of O(1) for lookup on average no matter how big the data sets are in the table.

In a worst and rare case, the lookup time of the hash tables has a time complexity of O(n).

Dictionaries

More or less, every programming language has a way of referring to hash tables in their own terms.

But in Python, they are known as dictionaries. Dictionaries are the implementation of hash tables in Python.

They consist of multiple key-value pairs in which each key identifies and maps to their corresponding values.

Dictionaries in Python start and end with curly brackets.

Creating & Printing a Dictionary

#Defining the dictionary

employee = {"Name": "Peter Pan", 
            "Designation": "Store Manager",
            "Phone Number": 99011110222}

#The colon separates the key from its value. 

#Printing the dictionary

print(employee)

The above code will give you the following output:

{'Name': 'Peter Pan', 'Designation': 'Store Manager', 'Phone Number': 99011110222}

Accessing the Values

valueOne = employee["Name"] 
print(valueOne)

#or you can also use the method get()

valueTwo = employee.get("Designation")
print(valueTwo)

The above code will give you the following output:

Peter Pan 
Store Manager

Updating the Dictionary

employee = {"Name": "Peter Pan", 
            "Designation": "Store Manager",
            "Phone Number": 99011110222}


employee["Phone Number"] = 123456789

'''
Now once you print the updated dictionary. It will have the new phone number of the employee.
'''

print(employee) 

The above code will give you the following output:

{'Name': 'Peter Pan', 'Designation': 'Store Manager', 'Phone Number': 123456789}

Adding an Item

employee = {"Name": "Peter Pan", 
            "Designation": "Store Manager",
            "Phone Number": 99011110222}

employee["Age"] = 27
#Notice how adding an item is similar to updating the dictionary.

print(employee)

The above code will give you the following output:

{'Name': 'Peter Pan', 'Designation': 'Store Manager', 'Phone Number': 99011110222, 'Age': 27}

Removing Items

employee = {"Name": "Peter Pan", 
            "Designation": "Store Manager",
            "Phone Number": 99011110222}

#Use the pop() method to remove an item of its corresponding key.

employee.pop("Name")
print(employee) 

The above code will give you the following output:

{'Designation': 'Store Manager', 'Phone Number': 99011110222}

If you want to remove an item from the last in hash tables, then use the popitem() method.

employee = {"Name": "Peter Pan", 
            "Designation": "Store Manager",
            "Phone Number": 99011110222}

#Let's say we want to remove the last item.

employee.popitem()
print(employee)

The above code will give you the following output:

{'Name': 'Peter Pan', 'Designation': 'Store Manager'}

Conclusion

Hash tables are very efficient. Their time complexity is one of the reasons why it is the best data structure in my opinion.

In addition, compared to other programming languages, the implementation of hash tables in Python is fairly simple. You can use the dictionary data type for implementations.

Leave a Reply