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
EasyCreate 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"] = 30Updating an existing value:
my_dict["age"] = 31Deleting a key-value pair:
del my_dict["age"]Try it yourself
def update_employee_info(employee_dict, key, value):
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter