Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Adding and updating key-value pairs
๐ Scenario: You are managing a small store's inventory. You need to keep track of the products and their quantities using a dictionary.
๐ฏ Goal: Create a dictionary with initial products and quantities, add a new product, update the quantity of an existing product, and then display the final inventory.
๐ What You'll Learn
Create a dictionary called inventory with exact products and quantities
Add a new product with a specific quantity to inventory
Update the quantity of an existing product in inventory
Print the final inventory dictionary
๐ก Why This Matters
๐ Real World
Managing inventory is a common task in stores and warehouses to keep track of product quantities.
๐ผ Career
Knowing how to add and update key-value pairs in dictionaries is essential for data management and software development.
Progress0 / 4 steps
1
Create the initial inventory dictionary
Create a dictionary called inventory with these exact entries: 'apples': 10, 'bananas': 5, and 'oranges': 8.
Python
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Add a new product to the inventory
Add a new product 'pears' with quantity 12 to the inventory dictionary.
Python
Hint
Use square brackets [] with the new key to add it to the dictionary.
3
Update the quantity of an existing product
Update the quantity of 'bananas' in the inventory dictionary to 7.
Python
Hint
Use the key 'bananas' with square brackets to assign the new quantity.
4
Display the final inventory
Print the inventory dictionary to show all products and their quantities.
Python
Hint
Use the print() function to display the dictionary.
Practice
(1/5)
1. What happens when you use dictionary[key] = value in Python if the key already exists?
easy
A. A new key-value pair is added without changing the old one.
B. The value for that key is updated to the new value.
C. An error is raised because the key already exists.
D. The dictionary is cleared before adding the new pair.
Solution
Step 1: Understand dictionary key assignment
Using dictionary[key] = value sets 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 B
Quick 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
A. car.color = 'blue'
B. car.add('color', 'blue')
C. car('color') = 'blue'
D. car['color'] = 'blue'
Solution
Step 1: Recall dictionary syntax for adding pairs
Use dictionary[key] = value to add or update pairs.
A. {'apple': 'red', 'banana': 'yellow', 'cherry': 'red'}
B. {'apple': 'red', 'banana': 'green'}
C. {'apple': 'red', 'banana': 'green', 'cherry': 'red'}
D. SyntaxError
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 C
Quick 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
A. Using '==' instead of '=' to add/update key-value pair.
B. Missing quotes around key 'z'.
C. Dictionary keys must be integers, not strings.
D. print() cannot display dictionaries.
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 A
Quick 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
A. All of the above
B. inventory['apple'] += 2
C. inventory.update({'apple': inventory['apple'] + 2})
D. inventory['apple'] = inventory['apple'] + 2
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 A
Quick Check:
Multiple ways to update dictionary values [OK]
Hint: Multiple ways to update values, use any [OK]