About 4319 letters
About 22 minutes
A dictionary is implemented using a hash table, making it a highly efficient data structure. It supports fast operations for element lookup, insertion, and deletion.
Dictionary literals are defined using curly braces ({}) that wrap a group of key-value pairs, for example:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
The type annotation dict[str, int] means a dictionary with keys of type str and values of type int.
In the literal, 'Tom', 'Jerry', and 'Spike' are keys, and 88, 99, and 66 are values.
You can access elements by following the dictionary with square brackets and the key:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
print(score_list)
print("Jerry's score is", score_list['Jerry'])
When working with large datasets, dictionary lookups are not only simpler than list searches but also significantly faster:
# Storing the score list using a list of tuples
score_list:list[tuple[str, int]] = [('Tom', 88), ('Jerry', 99), ("Spike", 66)]
# Loop to find Jerry's score
index:int = 0
while index < len(score_list):
if score_list[index][0] == 'Jerry':
print("Jerry's score is", score_list[index][1])
break
index += 1
else:
print("Jerry's score not found")
Trying to access a key that doesn't exist will raise an error:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
print(score_list['Tuffy'])
You can use in to check if a key exists in the dictionary:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
print('Tuffy' in score_list)
print('Tuffy' not in score_list)
You can add or modify elements using square brackets with the key:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
score_list['Spike'] = 77 # Spike already exists, so its value is updated
score_list['Tuffy'] = 33 # Tuffy doesn't exist, so a new element is added
print(score_list)
You can delete elements using the pop method:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
score_list.pop('Jerry')
print(score_list)
Use the clear method to remove all elements:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
score_list.clear() # Clear the dictionary
print(score_list)
Use the update method to merge another dictionary:
schoole_score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
class1_score_list:dict[str,int] = {
'Tom': 77,
'Tuffy': 33,
}
schoole_score_list.update(class1_score_list) # Merge class1_score_list into schoole_score_list. Duplicate keys will be overwritten.
print(schoole_score_list)
Just like with lists, assigning a dictionary to another variable creates a reference. To avoid this, make a copy instead:
score_list:dict[str,int] = {
'Tom': 88,
'Jerry': 99,
'Spike': 66
}
shadow:dict[str,int] = score_list # Reference to the original dictionary
shadow['Spike'] = 77 # Modifying shadow also affects score_list
shadow2:dict[str,int] = dict(score_list) # Create a new dictionary
shadow2['Spike'] = 55 # Modifying shadow2 doesn't affect score_list
print(score_list)
print(shadow)
print(shadow2)
Created in 5/15/2025
Updated in 5/21/2025