About 4319 letters

About 22 minutes

#Python's dictionary

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
}
HashTable score_list score_list bucket1 Bucket 1 key: Tom value: 88 score_list->bucket1 Tom bucket2 Bucket 2 key: Jerry value: 99 score_list->bucket2 Jerry bucket3 Bucket 3 key: Spike value: 66 score_list->bucket3 Spike

The type annotation dict[str, int] means a dictionary with keys of type str and values of type int.

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'])

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

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")

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Check Key Existence with in

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'])

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Add or Modify Elements

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Delete Elements

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Clear the Dictionary

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Merge Dictionaries

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Copy a Dictionary

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)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Updated: 5/21/2025

Author: PlanC

Created: 5/15/2025