Welcome to The Coding College, your trusted resource for mastering programming concepts! Today, we’ll explore how to change dictionary items in Python. This skill is essential for managing and updating data dynamically in your programs.
Understanding Dictionary Items
A dictionary in Python stores data as key-value pairs. You can easily update or modify these pairs using specific techniques.
Example of a Dictionary:
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
Here, name
, age
, and city
are keys, while "Alice"
, 25
, and "New York"
are their respective values.
Changing Dictionary Items
To modify a dictionary item, simply assign a new value to an existing key.
Syntax:
dictionary[key] = new_value
Example 1: Update a Single Item
person = {"name": "Alice", "age": 25, "city": "New York"}
# Update the value of the 'age' key
person["age"] = 26
print(person)
Output:
{'name': 'Alice', 'age': 26, 'city': 'New York'}
Adding a New Key-Value Pair
If the key doesn’t exist in the dictionary, assigning a value to it creates a new key-value pair.
Example 2: Add a New Key
person = {"name": "Alice", "age": 25}
# Add a new key-value pair
person["city"] = "New York"
print(person)
Output:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
Updating Multiple Items
You can use the update()
method to modify multiple key-value pairs simultaneously.
Syntax:
dictionary.update({key1: value1, key2: value2, ...})
Example 3: Update Multiple Items
person = {"name": "Alice", "age": 25}
# Update multiple items
person.update({"age": 26, "city": "New York"})
print(person)
Output:
{'name': 'Alice', 'age': 26, 'city': 'New York'}
Modify Nested Dictionary Items
When dictionaries contain other dictionaries as values, you can access and modify the nested items using their keys.
Example 4: Modify a Nested Item
person = {
"name": "Alice",
"details": {
"age": 25,
"city": "New York"
}
}
# Update the 'age' inside the nested dictionary
person["details"]["age"] = 26
print(person)
Output:
{'name': 'Alice', 'details': {'age': 26, 'city': 'New York'}}
Best Practices for Changing Dictionary Items
- Check if a Key Exists: Use the
in
keyword to verify a key before updating.
if "city" in person:
person["city"] = "Los Angeles"
- Use Default Values for Missing Keys: Use the
get()
method to avoidKeyError
.
person["country"] = person.get("country", "USA")
- Leverage the
update()
Method: Update multiple items efficiently without repetitive assignments.
Practice Exercises
Exercise 1: Update an Item
Given the dictionary:
student = {"name": "John", "grade": "B", "age": 20}
- Change the value of
grade
to"A"
.
Exercise 2: Add a New Key-Value Pair
Using the same dictionary, add the key-value pair "city": "London"
.
Exercise 3: Modify a Nested Dictionary
Given the dictionary:
employee = {"id": 101, "info": {"name": "Sarah", "department": "HR"}}
- Update the value of
department
to"IT"
.
Why Learn with The Coding College?
At The Coding College, we prioritize hands-on learning and user-focused content. Mastering how to change dictionary items is a foundational skill that opens up endless possibilities in programming.
Conclusion
Python dictionaries are dynamic, allowing you to modify key-value pairs with ease. Whether updating single items, adding new pairs, or working with nested dictionaries, Python offers intuitive methods to handle it all.