Hashmap in Python
A hashmap, synonymous with a dictionary, efficiently links keys to their associated values. Essential in Python, understanding dictionaries equips one to utilize hashmaps effectively. Operating as indexed structures, hashmaps employ hash functions to determine storage locations within an array of buckets. Each unique, immutable key corresponds to a specific bucket, resembling labeled drawers in a cabinet storing related data like user information. The hash function's role is pivotal; it transforms keys into bucket indices. Ideally, distinct keys should yield unique indices, but occasional collisions, where multiple keys map to the same index, can occur. To manage collisions, techniques like appending values to lists within buckets or rehashing can be employed, ensuring data integrity within the hashmap.
Hash table vs Hashmap
Let's look at the differences between the hash table and hashmap in Python now.
Hash Table | HashMap |
---|---|
There are no null keys or values allowed. | Allows for one null key and many null values. |
Slow | Fast |
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.