Hashmap in Python

Hashmap or hashtable in python is like an array. Hashmap is a data structure in python that stores data in the form of key or value pairs.

In hashmap we can store any type of key, it can be a string or an integer of any type. Hashmaps are also good in fast searching more than an array, it is much better in aspects of deleting or editing a key value as well.

There are a few differences between hashmap and hashtable in python, the hashmap is a little slower than hashtable it is also non-synchronized while hashtable is.

The syntax for working on a hashmap is like using a dictionary, for creating a hashmap we use a dictionary, and the hashing function is used for generating key values of the dictionary.

Syntax

The syntax for creating hashmaps is like creating a dictionary so we can create a dictionary with the following hashmap functions:

The hashmap includes “set_val”, ”get_val”, and ”delete_val”  functions for inserting key values, getting key values, and deleting key values from the hashmaps.

Let’s discuss the working of hashmap functions.

Set_val(key, value)

In the set_val hashmap function key, value pairs are inserted, in case they are already inserted so the key value is updated automatically.

Get_val(key)

In the get_val() function a value is returned to the defined key position where it is mapped, in case the function could not find the mapped key position so an error occurred known as ”No Record found ”.

Delete_val(key)

 The delete_val() function is used to delete the mapping of a defined key if it exists.

For better understanding checkout the examples below.

Example 1

dict = {'Name': 'Peter', 'Age': 18}
print ("Value : %s" % 
dict.get('Name'))
print ("Value : %s" % 
dict.get('ID',"none"))

Code Explanation

In the above code, first, we created a dictionary, ‘Name’ and ‘Age’ are keys, and ‘Peter’, and ’18’ is their values. Then we used the get_val() function for getting the value of existing keys. Check out the result below.

Output

Value: Peter
Value: none

 Now let’s see how to insert or update a value in a dictionary.

Example 2

hash_dict={'Python' : '101' , 'PHP': '102' , 'Java': '103'}
hash_dict['Python'] = '105'   #Updating the value of Python key
hash_dict['C++'] = '104'  #inserting a key-value pair
print(hash_dict)

Code Explanation

In the above code we created a hashmap dictionary then we updated the value python key,

After that, we inserted a new key value-pair into the hashmap, and in the end, we printed out the dictionary. Check out the results below.

Output

{'Python': '105', 'PHP': '102', 'Java': '103', 'C++': '104'}

As you can see in the output above the value of the python key has been updated and a new value has been inserted into the dictionary.

For deleting a key value-pair from a dictionary we can use multiple functions like del(), pop(), etc . in the below example we used all these methods check out the example below.

Example 3

hash_dict={'Python' : '105' , 'PHP': '102' , 'Java': '103','C++':'104'}
del hash_dict['Python']   
hash_dict.pop('Java')
hash_dict.popitem()  
print(hash_dict)

Code Explanation

In the above example first, we created a dictionary then we used the del() function and mentioned the python key which we wanted to delete.

Then we used the pop() function for deleting the java key value-pair.

In the end, we used the popitem() function but we did not mention the key name because the pop item function deletes the last element or value from the dictionary.

Then we just printed the dictionary and check out the results below.

Output

{'PHP': '102'}

As you can see from the output above all those values have been deleted from the dictionary which we deleted using the del(), pop(), and popitem() functions.

Let’s move forward to see How to access key values, check out the example below.

Example 4

hash_dict={'Python' : '101' , 'PHP': '102' , 'Java': '103'}
#if you want to access only one key value from the dictionary, so just simply add the key whose value you want to know like this
hash_dict['Python']

Output

'101'

Conclusion

In this tutorial, we talked about hashmap in Python, we discussed what is hash mapping in python and how it works, we also discussed the hashmap functions briefly with examples, and then in the end we saw how to access a key from the dictionary.  

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below Thanks for reading.   

Leave a Comment