In this tutorial, we will learn how to initialize a Hashmap in Python. A hashmap, also known as a dictionary in Python, is a data structure that stores key-value pairs. Dictionaries are useful when we need to store and retrieve data quickly based on a specific key.
We will walk through the different ways to initialize a dictionary and how to perform basic operations such as adding and removing elements, looping through keys and values, and more.
Step 1: Creating an Empty Dictionary
To create an empty dictionary, simply use curly braces {}
or the dict()
constructor.
1 2 3 4 5 6 7 8 |
# Creating an empty dictionary using curly braces empty_dict = {} # Creating an empty dictionary using the dict() constructor empty_dict2 = dict() print(empty_dict) print(empty_dict2) |
Output:
{} {}
Step 2: Initializing a Dictionary with Key-Value Pairs
We can also initialize a dictionary with key-value pairs by placing them inside curly braces {}
and separating keys and values with colons.
1 2 3 4 5 6 7 8 |
# Initializing a dictionary with key-value pairs person = { "name": "John", "age": 25, "city": "New York" } print(person) |
Output:
{'name': 'John', 'age': 25, 'city': 'New York'}
Step 3: Adding Elements to a Dictionary
We can add new key-value pairs to a dictionary by specifying the key in square brackets []
and assigning a value to it.
1 2 |
person["country"] = "USA" print(person) |
Output:
{'name': 'John', 'age': 25, 'city': 'New York', 'country': 'USA'}
Step 4: Removing Elements from a Dictionary
We can remove an element from a dictionary by using the del
keyword, followed by the key of the element we want to remove.
1 2 |
del person["city"] print(person) |
Output:
{'name': 'John', 'age': 25, 'country': 'USA'}
Step 5: Looping Through Keys and Values in a Dictionary
We can iterate through the keys and values of a dictionary using a for loop with the items()
method, which returns a list of the dictionary’s key-value pairs represented as tuples.
1 2 |
for key, value in person.items(): print(f"{key}: {value}") |
Output:
name: John age: 25 country: USA
Step 6: Initializing a Dictionary Using Comprehensions
We can also conveniently create a dictionary using dictionary comprehensions, a concise way to generate dictionaries based on existing iterables.
1 2 3 |
# Square numbers dictionary squares = {x: x * x for x in range(1, 6)} print(squares) |
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Now that we’ve explored different ways to initialize and work with dictionaries, let’s see the full code example in one place.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Creating an empty dictionary using curly braces empty_dict = {} # Creating an empty dictionary using the dict() constructor empty_dict2 = dict() print(empty_dict) print(empty_dict2) # Initializing a dictionary with key-value pairs person = { "name": "John", "age": 25, "city": "New York" } print(person) # Adding new elements person["country"] = "USA" print(person) # Removing an element del person["city"] print(person) # Looping through keys and values for key, value in person.items(): print(f"{key}: {value}") # Initializing a dictionary using comprehensions squares = {x: x * x for x in range(1, 6)} print(squares) |
Conclusion
In this tutorial, we learned how to initialize dictionaries (hashmaps) in Python and perform basic operations such as adding, removing, and looping through key-value pairs. Implementing dictionaries in Python makes it easy to create and manage a mapping of keys to associated values, allowing us to improve our code efficiency and readability.