Every second of every day, our senses bring in way too much data than we can possibly process in our brains
– Peter Diamandis Chairman/CEO of, X-Prize Foundation.
This quote is enough to show the importance of data in today’s world. Not only is massive data being produced every second, but it also has a lot of variety.
We need different types of data structures to store this variety of data. Some data needs to be stored sequentially, while some data needs to be stored in key-value pairs. In this article, we will learn how to store data with the help of Python hashmaps.
Python hashmaps are a type of data structure that stores data in the form of a key-value pair. The primary purpose of the Python hashmaps is to retrieve data with the help of the key. Hashmaps are also termed Dictionaries in Python.
The intuition behind Python hashmaps
In hashmaps, the key is generated through hash functions. The main characteristics of this key are that it is unique and cannot be null (nothing). Also, hashmaps are non-synchronized. If we use it in a multithread environment, then more than one thread can access it and use the HashMap simultaneously.
It is not like we cannot use the index to retrieve values in a list or array, but the advantage of hashmaps is that we can make key strings, too. These types of keys are easy to remember, thus making retrieval easy and fast. Updating and deleting values also become easy.
In a programming language like Java, hashmaps are implemented using the collection framework. But in Python, hashmaps are implemented using the built-in data type – dictionary.
Implementation of Python HashMaps
In Python, dictionaries satisfy all the requirements, like unique keys and non-null values. Let us learn how to create hashmaps using some real-life examples.
When we need to make a call, we generally search for the name of the person, and as an output, we get the desired phone number. That is an example of hashmaps. The name is the key, and the phone number is the value. Now, suppose you have a list of phone numbers. And you want to make a phone call to a certain someone. How easy is the retrieval of that number from the list? Very difficult, I guess.
Also, it is challenging to remember phone numbers. Because as humans, we are not good at remembering numbers, but we are great at remembering names. Take another situation where you want to delete that phone number. If you know the name, you can easily remove the number. Now, that was a lot of explanation. Let’s begin the implementation.
phone_numbers={
'person1':909090909,
'person2':808080808,
'person3':707070707,
'person4':606060606,
'person5':505050505,
'person6':909090901,
'person7':808080802,
'person8':707070703,
'person9':606060604,
'person10':505050500
}
print(phone_numbers)
Output- {'person1': 909090909, 'person2': 808080808, 'person3': 707070707, 'person4': 606060606, 'person5': 505050505, 'person6': 909090901, 'person7': 808080802, 'person8': 707070703, 'person9': 606060604, 'person10': 505050500}
- If we want to search for a phone number of person1-
phone_numbers={
'person1':909090909,
'person2':808080808,
'person3':707070707,
'person4':606060606,
'person5':505050505,
'person6':909090901,
'person7':808080802,
'person8':707070703,
'person9':606060604,
'person10':505050500
}
print(phone_numbers.get('person1'))
# or print(phone_numbers['person1'])
Output- 909090909
Editing and Removing Value: Python Hashmaps
- Now, let’s suppose you want to remove/delete this value.
phone_numbers.pop('person1')
print(phone_numbers)
Output- {'person2': 808080808, 'person3': 707070707, 'person4': 606060606, 'person5': 505050505, 'person6': 909090901, 'person7': 808080802, 'person8': 707070703, 'person9': 606060604, 'person10': 505050500}
- You can also update the phone number if that person has changed the phone number.
phone_numbers['person3']=333333333
print(phone_numbers)
Output- {'person2': 808080808, 'person3': 333333333, 'person4': 606060606, 'person5': 505050505, 'person6': 909090901, 'person7': 808080802, 'person8': 707070703, 'person9': 606060604, 'person10': 505050500}
Another way of updating the value is by using the update().
phone_numbers.update({'person4':111111111})
print(phone_numbers)
Output- {'person2': 808080808, 'person3': 333333333, 'person4': 111111111, 'person5': 505050505, 'person6': 909090901, 'person7': 808080802, 'person8': 707070703, 'person9': 606060604, 'person10': 505050500}
- Adding values-
phone_numbers['person11']=111111111
print(phone_numbers)
Output- {'person2': 808080808, 'person3': 333333333, 'person4': 111111111, 'person5': 505050505, 'person6': 909090901, 'person7': 808080802, 'person8': 707070703, 'person9': 606060604, 'person10': 505050500, 'person11': 111111111}
Difference Between HashMaps and HashTable in Python
In Python, there is not much of a difference between Python hashmaps, hashtables, and dictionaries. But in other languages, one of the most common interview questions in hashmaps is it’s explaining its difference with hashtables.
- One of the crucial differences between hashtables and hashmaps is that hashtables are synchronized, but hashmaps are not. We already know the meaning of non-synchronized, and now let us see what synchronized data structure is. It means that only one thread can be modified by a hashtable at one point in time.
- HashMaps are faster and take much less memory when compared to HashTables.
Must Read:
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion-
Python Hashmaps are significant data structures when we have data in the form of a key-value pair. The implementation of hashmaps in Python is straightforward if we compare it to other languages because in Python, we have dictionaries to make the task easier.
Try to run the programs on your side, and let me know if you have any queries.
Happy Coding!