Hash Map
Hash map implementation in Python for competitive programming.
Hash Map in Python
In Python, a hash map is implemented using the built-in dict data structure. A hash map (or dictionary) allows you to store key-value pairs and provides efficient lookup, insertion, and deletion operations. Here are some examples of how to use a hash map in Python:
Creating a Hash Map
# Creating an empty hash map
my_hash_map = {}
# Creating a hash map with some key-value pairs
my_hash_map = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
}
print(my_hash_map) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Accessing Values
# Accessing values using keys
print(my_hash_map['key1']) # Output: value1
print(my_hash_map.get('key2')) # Output: value2
# Accessing a non-existent key returns None
print(my_hash_map.get('key4')) # Output: None
# Accessing a non-existent key with a default value
print(my_hash_map.get('key4', 'default')) # Output: default
Inserting and Updating Values
# Existing hash map
print(my_hash_map)
# Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
# Inserting a new key-value pair
my_hash_map['key4'] = 'value4'
print(my_hash_map)
# Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
# Updating an existing key-value pair
my_hash_map['key2'] = 'new_value2'
print(my_hash_map)
# Output: {'key1': 'value1', 'key2': 'new_value2', 'key3': 'value3', 'key4': 'value4'}
Deleting Key-Value Pairs
# Deleting a key-value pair using delete
del my_hash_map['key3']
print(my_hash_map) # Output: {'key1': 'value1', 'key2': 'new_value2', 'key4': 'value4'}
# Deleting a key-value pair using pop // pop returns the value of the removed key
removed_value = my_hash_map.pop('key2')
print(removed_value) # Output: new_value2
print(my_hash_map) # Output: {'key1': 'value1', 'key4': 'value4'}
# Pop the last inserted key-value pair
last_removed_value = my_hash_map.popitem()
print(last_removed_value) # Output: ('key4', 'value4')
print(my_hash_map) # Output: {'key1': 'value1'}
Checking for Keys
# Checking if a key exists in the hash map
print('key1' in my_hash_map) # Output: True
print('key3' in my_hash_map) # Output: False
res = 'key1' in my_hash_map
print(res) # Output: True
key_to_check = 'key3'
if key_to_check in my_hash_map:
print(f"{key_to_check} exists in the hash map.")
else:
print(f"{key_to_check} does not exist in the hash map.")
if key_to_check not in my_hash_map:
print(f"{key_to_check} does not exist in the hash map.")
else:
print(f"{key_to_check} exists in the hash map.")
Checking for Values
# Checking if a value exists in the hash map
print('value1' in my_hash_map.values()) # Output: True
print('value3' in my_hash_map.values()) # Output: False
Checking the Size of the Hash Map
# Checking the number of key-value pairs in the hash map
print(len(my_hash_map)) # Output: 1
Clearing the Hash Map
# Clearing all key-value pairs from the hash map
my_hash_map.clear()
print(my_hash_map) # Output: {}
Iterating Over a Hash Map
# Iterating over keys
for key in my_hash_map:
print(key) # Output: key1, key2
# Iterating over values
for value in my_hash_map.values():
print(value) # Output: value1, new_value2
# Iterating over key-value pairs
for key, value in my_hash_map.items():
print(f"{key}: {value}") # Output: key1: value1, key2: new_value2
Hash maps are a powerful data structure that can be used in various competitive programming problems, such as counting frequencies, grouping items, and implementing caches. Understanding how to use hash maps effectively can help you solve problems more efficiently.
Related Posts
For Looping in Python
Using for loops to manipulate arrays in Python.
While Looping in Python
Using while loops to manipulate arrays in Python.
Assignments
Assignments in Python for competitive programming.
Data Types
Data types in Python for competitive programming.