4 minute read

Introduction

In Python, both dictionaries and hashmaps are data structures used to store key-value pairs. However, there are some fundamental differences between the two that are important to understand. In this article, we will take a closer look at what dictionaries and hashmaps are, and how they differ.

Dictionaries

A dictionary is a built-in data structure in Python that uses a hash table to store key-value pairs. It is defined by enclosing the key-value pairs in curly braces {} and separating them with commas. For example, the following code creates a dictionary with three key-value pairs:

my_dict = {'a':1, 'b':2, 'c':3}

Dictionaries in Python are implemented in C, which makes them highly efficient in terms of memory and time complexity. They have an average time complexity of O(1) for operations such as inserting and retrieving elements.

The keys in a dictionary are hashed and the resulting hash values are used to index the key-value pairs in a hash table. Dictionaries are also ordered since Python 3.7, which means that the key-value pairs are stored in a specific order.

Hashmaps

A hashmap, on the other hand, is not a built-in data structure in Python, but it can be implemented using a dictionary or a list. It is a data structure that uses a hash function to map keys to values.

A hash function is a mathematical function that takes an input (or ‘message’) and returns a fixed-size string of characters, which is called the ‘hash value’.

In Python, the built-in hash() function can be used to create a hashmap. For example, the following code creates a hashmap with three key-value pairs:

my_hashmap = {hash('a'):1, hash('b'):2, hash('c'):3}

The hash() function returns an integer value that represents the object passed to it. In the case of a hashmap, the function is used to generate a unique hash value for each key, which is then used as the index for the key-value pair in the hashmap.

Hashmaps are unordered, meaning the key-value pairs are not stored in a specific order. However, they have the advantage of being able to handle more data types as keys.

Example

Given this hashmap, we can code it in Python such as:

my_hashmap = {hash('a'):1, hash('b'):2, hash('c'):3}
print(my_hashmap)

If we print this hashmap, we get the following output:

{4748209815333644220: 1, -7442019237409972376: 2, -2789995480227219980: 3}

It is important to note that the hash() function returns an integer which remains constant within an individual Python process but it may change next time Python is invoked. This is intended to provide protection against hacking, but it may not be what you’re looking for.

Differences

The main difference between a dictionary and a hashmap in Python is that a dictionary uses a built-in hash table to store key-value pairs, while a hashmap uses a user-defined hash function to map keys to values.

Dictionaries use the built-in hash function to generate a unique hash value for each key which is then used to index the key-value pair in the hash table. This makes dictionary operations efficient in terms of memory and time complexity.

Hashmaps, on the other hand, use a user-defined hash function, which can be implemented using the built-in hash() function or a custom function. This makes them less efficient in terms of memory and time complexity, but more versatile in handling different data types as keys.

Dictionaries are ordered and have a specific order for the key-value pairs, while hashmaps are unordered.

Conclusion

In conclusion, dictionaries and hashmaps are both useful data structures for storing key-value pairs in Python, but they have some fundamental differences.

Dictionaries are built-in and use a hash table to store key-value pairs, making them more efficient and ordered. Hashmaps, on the other hand, can be implemented using a dictionary or a list and use a user-defined hash function, making them unordered but more versatile in handling different data types as keys.

Understanding these differences will help you choose the right data structure for your specific use case and make your code more efficient.

We hope this article was useful to you.