In Python, the equivalent of a HashMap is known as a dictionary. A HashMap data structure stores key-value pairs where each key is unique. The keys are mapped to values using a hashing function, making the data structure efficient for operations like inserting, retrieving, and deleting elements. This article explores the HashMaps (dictionaries) work in Python and demonstrates their key features through examples.
Introduction to HashMap in Python
A Hash Map is a data structure well known as a dictionary in the Python language environment. This is one of the most popular data structures known to everyone. The databases store data like arrays pointing to values connected with keys. As will be seen when comparing it with a list, while elements within lists are referenced through positions, dictionaries use keys to access values. Python uses a hash table in the dictionary, which is why the look-up time for the dictionary is almost negligible.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Creating a HashMap(Dictionary)
In Python, creating a HashMap is straightforward. We can use curly braces {} or the dict() function to create a new dictionary.
# Creating an empty dictionary
hashmap = {}
# Creating a dictionary with initial key-value pairs
hashmap = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(hashmap)
Output
{'name': 'Alice', 'age': 25, 'city': 'New York'}
Adding Elements to a HashMap
This section will show how to add HashMap elements in Python.
# Creating an empty dictionary
hashmap = {}
hashmap = {'name': 'Alice', 'age': 25, 'city': 'New York'}
hashmap['country'] = 'USA'
print(hashmap)
Output
{'name': 'Alice', 'age': 25, 'city': 'New York', 'country': 'USA'}
Accessing Values in a HashMap
We can access the value of a specific key by using the key inside square brackets []. If the key does not exist, Python raises a KeyError. Alternatively, we can use the get() method to safely access values, which returns None if the key is absent.
Program
hashmap = {}
hashmap = {'name': 'Alice', 'age': 25, 'city': 'New York'}
hashmap['country'] = 'USA'
print(hashmap['name'])
print(hashmap.get('age'))
Output
Alice
25

82.9%
of professionals don't believe their degree can help them get ahead at work.
Modifying Values in a HashMap
Modifying values in a HashMap is simple. We can assign a new value to an existing key.
Program
hashmap = {'name': 'Alice', 'age': 25, 'city': 'New York'}
hashmap['country'] = 'USA'
print(hashmap)
Output
{'name': 'Alice', 'age': 25, 'city': 'New York', 'country': 'USA'}
Deleting Elements from a HashMap
We can remove elements from a HashMap using the del statement or the pop() method. The pop() method returns the value of the key being removed, whereas del simply removes the key-value pair without returning the value.
Program
hashmap ={}
hashmap= {'name':'Alice', 'age':25, 'city': 'New York'}
del hashmap['city']
removed_value = hashmap.pop('age')
print(hashmap)
print(removed_value)
Output
{'name': 'Alice'}
25
HashMap Operations and Methods
Python dictionaries offer various built-in methods to work with HashMas effectively.
- len(): This method returns the key-value pairs in the dictionary.
- keys(): This method returns all keys in the dictionary.
- values(): This method returns all values in the dictionary.
- items(): This method returns all key-value pairs as tuples.
- update(): The dictionary is updated with elements from another dictionary.
Program
hashmap = {}
hashmap = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(len(hashmap))
print(hashmap.keys())
print(hashmap.values())<strong> </strong>
print(hashmap.items())
hashmap.update({'gender': 'female', 'city': 'New York'})
print(hashmap)
Output
3
dict_keys(['name', 'age', 'city'])
dict_values(['Alice', 25, 'New York'])
dict_items([('name', 'Alice'), ('age', 25), ('city', 'New York')])
{'name': 'Alice', 'age': 25, 'city': 'New York', 'gender': 'female'}
HashMap Applications in Python
HashMap is widely used in Python Programming because it is efficient in storing and retrieving data. Let’s look at some applications that use HashMap in Python.
- Counting frequency: Counting occurrences of elements in a dataset.
- Caching: Storing the results of expensive operations to avoid repeating them.
- Symbol tables: We can also use a hashmap to store the variable names and values in interpreters or compilers.
- Graph algorithms: It stores the adjacent lists for graphs.
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, which is O(1) time.
Hashtable vs Hashmap
The comparison between Hashtable and HashMap in the context of Python.
| Feature | Hash Table | HashMap(Python Dictionary) |
| Definition | The data structure that stores key-value pairs using a hashing mechanism | Python’s dict is a built-in data type that functions as a hash map using a hash table under the hood |
| Thread Safety | Thread-safe external synchronization is not required when using custom implementations. | The thread-safe Python’s dict is not thread-safe, but there are threadsafe alternatives like collections.defaultdict combined with threading.Lock |
| Null Key/ Value | The custom implementation may or may not allow None keys or values | It allows none as both a key and value in Python dictionaries. |
| Performance | The custom implementation performance depends on the algorithm | Python’s dict is highly optimized for performance. Hash lookups, inserts, and deletes have an average time complexity of O(1). |
| Implementation | It must be manually implemented or imported from a third-party library. | It is built-in and readily available in Python as dict |
Also Read: Python Interview Questions and Answers
Conclusion
This article introduces the HashMap (dictionary) in Python. It is a flexible and effective data structure for storing and retrieving information and using keys and values. It provides a comprehensive list of techniques and convenient notation. Python dictionaries are relevant to many programming tasks. Realizing how to work with HashMaps will enhance your problem-solving skills and make a huge difference in your source code. Explore Python programming in detail with Hero Vired’s Certificate Program in DevOps & Cloud Engineering, offered in collaboration with Microsoft.
What is the difference between a HashMap in Java and a dictionary in Python?
Are Python dictionaries ordered?
Can a Python dictionary have duplicate values?
How do you update a dictionary with another dictionary?
How do you create an empty dictionary?
Are there any alternatives to HashMap in Python?
Updated on February 17, 2025
