Menu
Coddy logo textTech

Modifying Dictionaries

Part of the Logic & Flow section of Coddy's Python journey — lesson 10 of 78.

Dictionaries in Python are not static; you can modify them after they are created. You can add new key-value pairs, update existing ones, or delete them.

Adding a new key-value pair:

# Start with an empty dictionary
my_dict = {}

# Add a new key-value pair
my_dict["name"] = "Alice"
my_dict["age"] = 30

print(my_dict)
# Output: {'name': 'Alice', 'age': 30}

Updating an existing value:

# Update the age
my_dict["age"] = 31

print(my_dict)
# Output: {'name': 'Alice', 'age': 31}

Deleting a key-value pair:

# Delete the age
del my_dict["age"]

print(my_dict)
# Output: {'name': 'Alice'}

In these examples, we first add a new key-value pair to an empty dictionary. Then, we update the value of an existing key. Finally, we delete a key-value pair using the del keyword.

challenge icon

Challenge

Easy

Create a function named update_employee_info that takes three parameters: employee_dict (a dictionary), key (a string), and value. The function should update the employee_dict with the new key and value. If the key already exists, its value should be updated. If the key does not exist, a new key-value pair should be added. The function must return the updated dictionary.

Important: Make sure to modify the original dictionary and return it. Use the square bracket notation dict[key] = value to add or update entries.

Cheat sheet

Dictionaries in Python can be modified after creation. You can add new key-value pairs, update existing ones, or delete them.

Adding a new key-value pair:

my_dict = {}
my_dict["name"] = "Alice"
my_dict["age"] = 30

Updating an existing value:

my_dict["age"] = 31

Deleting a key-value pair:

del my_dict["age"]

Try it yourself

def update_employee_info(employee_dict, key, value):
    # Write code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow