Python HashMap Implementation Using Dictionary

In this tutorial you’ll learn how to implement hashmap in python using dictionary.

What is Hash Table or Hash Map?

In short, Hash table is a data structure which stores data in an associative manner. In this table, our data is stored in a array format, where each data value have a unique key to represent the data. So in this way, access of data becomes very fast if we know the key of the desired data. So in Hash Table, insertion and search operations are very fast irrespective of the size of the data.

How to Implement Hash Map in Python?

Well if you’re looking for implementation of hash map in python then the answer is Dictionary. Python Dictionary is a built-in type that supports key-value pairs. Basically Dictionary is like a list but instead of an integer index it can be of any type. Another thing to note that a dictionary is un-ordered, it may not show the items in the order it was defined. So in this article we’ll learn about dictionaries and all the operations that we can perform on a dictionary.

Python HashMap Implementation Using Dictionary

Creating a Dictionary

Let’s say we want to create a dictionary called phonebook where names will be used as keys and their phone numbers as values to the keys, then our code will be:

Here phonebook  is the name of the dictionary and name 1, name 2 and name 3 are unique keys and each key represent to a phone number (data). Here the keys and data both are in string format but we can use any other datatype like int. 

Accessing Data from Dictionary

We can print all the entries by using for loop or we can print a specific value using its key. Let’s say we want access the phone number of name 1.

Output:

9100004000

Or if you want to print all of the entries then the code will be.

Output:

name 3 – 9100004000
name 2 – 9104043000
name 1 – 9100000000 

Here k and v are loop variables, where k is used for key and v is for their values.

Note: Dictionary can’t have duplicate entry. If more than one keys (having same name) are present in the dictionary then the last entry will be stored.

Add New Entry or Update Existing One Into Dictionary

Output:

name 1 – 9100000000
name 4 – 0681234323
name 2 – 0581234323
name 3 – 9100004000

Here phonebook[key] = value can be used for both, to insert a new entry and also to update an existing entry. If the key is present in the dictionary then it will update the the value of that key with given value, if key doesn’t exist then It will add a new entry into the dictionary.

Deleting Entries from Dictionary

To delete a specific entry from the dictionary:

Output:

name 1 – 9100000000
name 3 – 9100004000

Here we’re deleting a particular entry from the dictionary phonebook using del statement.

Delete All Entries from Dictionary

Output:

phonebook  = {} 

Here clear() method is used to clear all the entries in dictionary. The dictionary will be empty.

Delete Entire Dictionary

Output:

NameError: name ‘phonebook’ is not defined

So del statement can be also used to delete the entire dictionary instead of a specific element.

Find Total Number of Entries in Dictionary

Output:

No. of entries = 3

Here class_a is a dictionary which have students roll numbers as keys and name as values. Now to count the number of entries, we’re using len() function here. Which will count the number of elements in the dictionary class_a and will return the number to the variable total_items.

There are several other methods used by dictionaries like:

Let’s say our dictionary name is dict.

  • dict.copy() – returns a copy of dictionary dict.
  • dict.keys() – returns a list of keys present in dict.
  • dict.values() – returns a list of values present in dict.
  • dict.items() – returns a list of tuple pairs (key values) present in dict.
  • cmp(dict1, dict2) – compare both of the dictionaries and returns true, false. The function cmp() can be used only in Python 2.

for more information on dictionaries in Python please visit https://docs.python.org/3/tutorial/datastructures.html

Comment below if you’ve any problem or suggestion related to python hashmap implementation using dictionary.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.