Changing values in a nested dictionary can be a little bit tricky, especially for those who are new to Python programming.
A nested dictionary is simply a dictionary inside another dictionary. In this tutorial, we are going to learn how to change a value in a nested dictionary using Python.
To begin with, let us assume that we have a nested dictionary as shown in the code below:
1 2 3 4 5 |
{ 'student1': {'name': 'John', 'age': 20, 'courses': ['Mathematics', 'Chemistry', 'Physics']}, 'student2': {'name': 'Jane', 'age': 22, 'courses': ['Biology', 'History', 'Geography']}, 'student3': {'name': 'Mary', 'age': 19, 'courses': ['Music', 'English', 'Swahili']} } |
In the example above, we have a dictionary that contains three nested dictionaries. Each nested dictionary represents a different student and contains information such as ‘name’, ‘age’, and a list of ‘courses’ that the student is taking.
Step 1: Access the Nested Dictionary
To change a value in a nested dictionary, first, we need to access the dictionary that contains the value we want to change. In the example above, we can access the nested dictionary for ‘student1’ using the code below:
1 |
student1_dict = my_dict['student1'] |
This code will store the nested dictionary for ‘student1’ in a variable called ‘student1_dict’. Next, we can print the contents of the nested dictionary using the code below:
1 |
print(student1_dict) |
This code will output the following:
{'name': 'John', 'age': 20, 'courses': ['Mathematics', 'Chemistry', 'Physics']}
Step 2: Change the Value in the Nested Dictionary
Once we have accessed the nested dictionary, we can change the value of any key in the dictionary. For example, if we want to change the value for the ‘age’ key for ‘student1’ to 21, we can use the code below:
1 |
student1_dict['age'] = 21 |
This code will change the value for the ‘age’ key in the ‘student1’ nested dictionary to 21. Next, we can print the contents of the nested dictionary using the code below:
1 |
print(student1_dict) |
This code will output the following:
{'name': 'John', 'age': 21, 'courses': ['Mathematics', 'Chemistry', 'Physics']}
Step 3: Update the Original Dictionary
Finally, we need to update the original dictionary with the new value. We can do this by using the code below:
1 |
my_dict['student1'] = student1_dict |
This code will update the original dictionary with the new nested dictionary that contains the updated value for the ‘age’ key. Next, we can print the contents of the original dictionary using the code below:
1 |
print(my_dict) |
This code will output the following:
{ 'student1': {'name': 'John', 'age': 21, 'courses': ['Mathematics', 'Chemistry', 'Physics']}, 'student2': {'name': 'Jane', 'age': 22, 'courses': ['Biology', 'History', 'Geography']}, 'student3': {'name': 'Mary', 'age': 19, 'courses': ['Music', 'English', 'Swahili']} }
As we can see from the output above, the value for the ‘age’ key in the nested dictionary for ‘student1’ has been successfully changed to 21.
That’s it! You have successfully learned how to change a value in a nested dictionary using Python.
At the end of the post, display the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
my_dict = { 'student1': {'name': 'John', 'age': 20, 'courses': ['Mathematics', 'Chemistry', 'Physics']}, 'student2': {'name': 'Jane', 'age': 22, 'courses': ['Biology', 'History', 'Geography']}, 'student3': {'name': 'Mary', 'age': 19, 'courses': ['Music', 'English', 'Swahili']} } student1_dict = my_dict['student1'] print(student1_dict) student1_dict['age'] = 21 print(student1_dict) my_dict['student1'] = student1_dict print(my_dict) |