Hashmap in Python
Learn via video course
Overview
Data is accessed and stored in a variety of ways. Hash tables are an important type of data. Dictionary, or the built-in data type, is used to access hash tables. In this data structure the information is stored in the form of key-value pairs. Key can be used as a reference to retrieve the stored data from the hash table. Due to this reason the hashmap table is also know as the look-up table data structure.
Scope
After going through this article:
- The reader will understand about the hashmap in python and how to design it.
- The difference between the hashmap and hash table will be more clear.
- Reader will understood about the functioning and time complexity of the hashmap.
Introduction
In computer science, a hash table, also known as a hashmap, is one type of data structure which maps the keys to value pairs. It works by calculating an index value with a function, and then storing the elements to be searched, removed, inserted and etc. This simplifies and accelerates data access. Key-value pairs are stored in hash tables, with the key generated using a hash function.
Hash tables and has maps are implemented using Python's built-in dictionary data type.
The keys of a dictionary are generated via a hashing function in Python. The elements of a dictionary are not sorted and can be changed. The names of employees and their IDs, or the names of students and their IDs, are mapped.
Hash table vs Hashmap
Let's look at the differences between the hash table and hashmap in Python now.
Hash Table | HashMap |
---|---|
Allows for one null key and many null values. | There are no null keys or values allowed. |
Fast | Slow |
Synchronized | Non-Synchronized |
Design HashMap in Python
Let's say we want to create a HashMap that doesn't use any of the built-in hash table libraries. The following are the various functions that will be available:
- get_val(key): This function returns the value to which the given key is mapped, or "No record found" if no mapping for the key exists in this map.
- set_val(key,val): A key-value pair is added to the hash map. Update the value if it already exist in the hash map.
- delete_val(key): If the hash map already has the mapping for the key, this method removes it.
Understanding HashMap
In Python, there are two ways to create and initialise dictionaries. Some of them are as follows:
- Using the dict() function
- Using the curly braces ({})
Let's have a look at the initial strategy.
The code above returns a type dictionary after creating a new dict dictionary.
Now let's have a look on another approach to create a dictionary.
Let's look at some of the operations that dictionaries can perform.
How to Use Key Values?
The values of a dictionary can be accessed by typing the dictionary's name into a square box, followed by the key name. For example, simple dict[Name] will return the value that is Aditya, sample [Age], for example, will yield a value that is 22, and so on.
Functions to be used
Another way to acquire the values of a dictionary is to use a combination of built-in functions. A dictionary, for example, is shown below.
The function sample_dict.keys() returns a list of all the current keys in the dictionary. The output in this case will be Name, Age, and City.
Sample_dict.values(), on the other hand, returns all of the dictionary's values for each key. In this scenario, Aditya, 22 and Las-Vegas will be the results.
Finally, to get values, use the get() command. For example, sample_dict.get(Name) will return the value Aditya.
Making use of Loops
Another way to extract values from a dictionary is to use loops to iterate through them. Consider the following example from the previous dictionary:
Let's go over this dictionary again.
All of the keys in the dictionary are printed out using the lines of code above.
All of the values of dictionary's keys are printed by using the code above. Last but not least:
The above lines of code prints all of the elements in the dictionary's key and value.
Changes to dictionary values are possible. Because dictionaries are mutable, their entries can be changed as needed. For example, if I want to change City in sample dict from Las-Vegas to Los-Angeles, I can do so in the following ways:
Delete entries of a dictionary
There are a few ways to remove items from a dictionary. To delete an entire item or a key and value pair, for example, we can use the below line of code.
This above line of code will remove Name: Aditya pair from the dictionary.
Just the value, not the key, can be deleted with the pop() function.
The above code removes the key element while preserving the value of the Name key. You're probably wondering what the current value of Name's key is. It currently corresponds to a null value.
There's also a way to remove the most lately entered entry from the dictionary. This can be accomplished with the popitem() method.
clear() function can be used to clear all items of the dictionay. Using sample to clear the dictionary. All entries in the dictionary are removed using clear().
Time Complexity
It takes the same amount of time to hash and access memory indexes. As a result, the search difficulty of a hash map is constant time that is O(1) time.
Conclusion
- Hashmap in python are used for fast insertion, deletion and look-up.
- It has the high storage capacity that is it can store the large datasets with high number of items.
- Value of the corresponding keys can be accessed very quickly using built-in functions.
- Hashmaps can be sorted using lambda and itemgetter functions.