We add or update key-value pairs to store or change information in a dictionary. This helps keep data organized and easy to find.
Adding and updating key-value pairs in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
dictionary[key] = value
This syntax works for both adding new pairs and updating existing ones.
If the key exists, the value is updated. If not, a new pair is added.
Examples
Python
my_dict = {}
my_dict['color'] = 'blue' # Adds a new key-value pairPython
my_dict = {'color': 'blue'}
my_dict['color'] = 'red' # Updates the value for 'color'Python
my_dict = {'age': 25}
my_dict['name'] = 'Alice' # Adds a new key 'name'Sample Program
This program starts with a dictionary holding a name and age. It adds a city and updates the age. Finally, it prints the updated dictionary.
Python
person = {'name': 'John', 'age': 30}
# Add a new key-value pair
person['city'] = 'New York'
# Update an existing key's value
person['age'] = 31
print(person)Important Notes
Using the same syntax for adding and updating keeps code simple and clear.
Keys must be unique; updating replaces the old value.
Values can be any data type, like numbers, strings, lists, or even other dictionaries.
Summary
Use dictionary[key] = value to add or update key-value pairs.
Adding creates a new pair if the key is not present.
Updating changes the value of an existing key.
Practice
1. What happens when you use
dictionary[key] = value in Python if the key already exists?easy
Solution
Step 1: Understand dictionary key assignment
Usingdictionary[key] = valuesets the value for the given key.Step 2: Behavior when key exists
If the key exists, the old value is replaced with the new one.Final Answer:
The value for that key is updated to the new value. -> Option BQuick Check:
Assigning existing key updates value [OK]
Hint: Assigning to existing key updates value, no error [OK]
Common Mistakes:
- Thinking it adds a duplicate key
- Expecting an error on existing key
- Assuming dictionary clears automatically
2. Which of the following is the correct syntax to add a new key-value pair
'color': 'blue' to a dictionary named car?easy
Solution
Step 1: Recall dictionary syntax for adding pairs
Usedictionary[key] = valueto add or update pairs.Step 2: Check each option
car['color'] = 'blue' uses correct syntax:car['color'] = 'blue'.Final Answer:
car['color'] = 'blue' -> Option DQuick Check:
Use square brackets for keys [OK]
Hint: Use square brackets and equals sign to add/update [OK]
Common Mistakes:
- Using parentheses instead of brackets
- Trying to call add() method which doesn't exist
- Using dot notation which works only for objects
3. What is the output of this code?
fruit_colors = {'apple': 'red', 'banana': 'yellow'}
fruit_colors['banana'] = 'green'
fruit_colors['cherry'] = 'red'
print(fruit_colors)medium
Solution
Step 1: Update existing key 'banana'
The value for 'banana' changes from 'yellow' to 'green'.Step 2: Add new key 'cherry'
The pair 'cherry': 'red' is added to the dictionary.Final Answer:
{'apple': 'red', 'banana': 'green', 'cherry': 'red'} -> Option CQuick Check:
Update and add keys correctly [OK]
Hint: Updating key changes value; new key adds pair [OK]
Common Mistakes:
- Assuming old value stays after update
- Forgetting to add new key
- Expecting syntax error from valid code
4. Find the error in this code snippet:
data = {'x': 1, 'y': 2}
data['z'] == 3
print(data)medium
Solution
Step 1: Identify the operator used for assignment
The code uses '==' which is a comparison operator, not assignment.Step 2: Correct operator for adding/updating dictionary
Use '=' to assign value to a key in dictionary.Final Answer:
Using '==' instead of '=' to add/update key-value pair. -> Option AQuick Check:
Use '=' to assign values [OK]
Hint: Use single '=' to assign, not '==' [OK]
Common Mistakes:
- Confusing '==' with '='
- Thinking keys must be numbers
- Believing print can't show dictionaries
5. You have a dictionary
inventory = {'apple': 5, 'banana': 3}. You want to add 2 more apples to the count. Which code correctly updates the inventory?hard
Solution
Step 1: Understand ways to update dictionary values
You can update a value by direct assignment, augmented assignment, or using update() method.Step 2: Check each option
inventory['apple'] = inventory['apple'] + 2 adds 2 and assigns directly. inventory['apple'] += 2 uses += operator. inventory.update({'apple': inventory['apple'] + 2}) uses update() with new value.Step 3: Confirm all methods work
All three correctly increase 'apple' count by 2.Final Answer:
All of the above -> Option AQuick Check:
Multiple ways to update dictionary values [OK]
Hint: Multiple ways to update values, use any [OK]
Common Mistakes:
- Thinking only one method works
- Forgetting to add current value before assignment
- Using update() incorrectly without new value
