Nested Dictionaries in Python
Nested Dictionaries
We already studied about the concept dictionaries and its overview in the previous section. Here we are going to learn about nested dictionaries and its overview which is almost similar to dictionaries concept.
Dictionary
In Python, a dictionary is a collection of items which are unordered. A dictionary has a key:value pair and those are enclosed within a curly braces { }.
Ex
>>> d={“today”:”Sunday”,”week”:”2″}
>>> d {‘today’: ‘Sunday’, ‘week’: ‘2’} >>> d.keys( ) dict_keys([‘today’, ‘week’]) >>> d.values( ) dict_values([‘Sunday’, ‘2’]) |
Nested Dictionary
In python nested dictionary is a collection of dictionaries into a single dictionary. In short, it is a dictionary inside a dictionary.
Syntax
>>> nested_dict = { ‘dictA’: {‘key_1’: ‘value_1′},’dictB’: {‘key_2’: ‘value_2’}} |
Here, the nested_dict is a nested dictionary with the dictionary dictA and dictB. They are two dictionaries each having its own key and value.
Creating Nested Dictionary
Creating a nested dictionary is almost similar as creating a dictionary. In the key value pair of a dictionary the value may be another dictionary. In short, it is a dictionary inside a dictionary.
>>> n_dict = { ‘D1’: {‘Name’: ‘Sudhakar’},’D2′: {‘Place’: ‘Chennai’}}
>>> n_dict {‘D2’: {‘Place’: ‘Chennai’}, ‘D1’: {‘Name’: ‘Sudhakar’}} |
Here we can observe that the key-value pair order is not same, since the dictionary will have unordered collection of items.
Adding elements into a Nested Dictionary
We can add elements into a nested dictionary in two ways. One is we can directly add the elements into a nested dictionary while creating a nested dictionary. Another way is shown in below example.
>>> n_dict[‘d1’]={ } #Creating an empty dictionary
>>> n_dict[‘d1’][“Student_1″]=”Sudhakar” #Inserting elements into it. >>> n_dict[‘d1’][“Dept”]=”CSE” >>> n_dict[‘d1’][“Gender”]=”Male” >>> n_dict[‘d1’] {‘Student_1’: ‘Sudhakar’, ‘Gender’: ‘Male’, ‘Dept’: ‘CSE’} >>> n_dict[‘d2’]={ } >>> n_dict[‘d2’][“Student_2″]=”Rathnam” >>> n_dict[‘d2’][“Dept”]=”ECE” >>> n_dict[‘d2’][“Gender”]=”Male” >>> n_dict[‘d2’] {‘Student_2’: ‘Rathnam’, ‘Gender’: ‘Male’, ‘Dept’: ‘ECE’} >>> n_dict[‘d3’]={ } >>> n_dict[‘d3’][“Student_3″]=”Ram” >>> n_dict[‘d3’][“Dept”]=”EEE” >>> n_dict[‘d3’][“Gender”]=”Male” >>> n_dict[‘d3’] {‘Gender’: ‘Male’, ‘Student_3’: ‘Ram’, ‘Dept’: ‘EEE’} >>> >>> n_dict {‘d1’: {‘Student_1’: ‘Sudhakar’, ‘Gender’: ‘Male’, ‘Dept’: ‘CSE’}, ‘d3’: {‘Gender’: ‘Male’, ‘Student_3’: ‘Ram’, ‘Dept’: ‘EEE’}, ‘d2’: {‘Student_2’: ‘Rathnam’, ‘Gender’: ‘Male’, ‘Dept’: ‘ECE’}} |
The following is another way to add elements into a nested dictionary.
>>> n_dict={‘d1’: {‘Student_1’: ‘Sudhakar’, ‘Gender’: ‘Male’, ‘Dept’: ‘CSE’},’d2′: {‘Student_2’: ‘Rathnam’, ‘Gender’: ‘Male’, ‘Dept’: ‘ECE’},’d3′: {‘Student_3’: ‘Ram’, ‘Gender’: ‘Male’, ‘Dept’: ‘EEE’}}
>>> n_dict {‘d2’: {‘Student_2’: ‘Rathnam’, ‘Dept’: ‘ECE’, ‘Gender’: ‘Male’}, ‘d1’: {‘Student_1’: ‘Sudhakar’, ‘Dept’: ‘CSE’, ‘Gender’: ‘Male’}, ‘d3’: {‘Dept’: ‘EEE’, ‘Student_3’: ‘Ram’, ‘Gender’: ‘Male’}} |
Accessing elements from Nested Dictionary
We can access the elements from a nested dictionary by using indexing ‘[ ]’. It is illustrated for the same above example.
>>> n_dict={‘d1’: {‘Student_1’: ‘Sudhakar’, ‘Gender’: ‘Male’, ‘Dept’: ‘CSE’},’d2′: {‘Student_2’: ‘Rathnam’, ‘Gender’: ‘Male’, ‘Dept’: ‘ECE’},’d3′: {‘Student_3’: ‘Ram’, ‘Gender’: ‘Male’, ‘Dept’: ‘EEE’}}
>>> n_dict[‘d1’][‘Student_1’] ‘Sudhakar’ >>> n_dict[‘d2’][‘Dept’] ‘ECE’ >>> n_dict[‘d1’][‘Dept’] ‘CSE’ >>> n_dict[‘d3’][‘Student_3’] ‘Ram’ >>> n_dict[‘d3’][‘Gender’] ‘Male’ |
Updating a Nested Dictionary
The following example describes how to update a dictionary in a nested dictionary.
>>> n_dict[‘d3’][‘Student_3’]=”John”
>>> n_dict {‘d3’: {‘Gender’: ‘Male’, ‘Student_3’: ‘John’, ‘Dept’: ‘EEE’}, ‘d2’: {‘Gender’: ‘Male’, ‘Student_2’: ‘Rathnam’, ‘Dept’: ‘ECE’}, ‘d1’: {‘Gender’: ‘Male’, ‘Dept’: ‘CSE’, ‘Student_1’: ‘Sudhakar’}} >>> >>> n_dict[‘d2’][‘Dept’]=”IT” >>> n_dict {‘d3’: {‘Gender’: ‘Male’, ‘Student_3’: ‘John’, ‘Dept’: ‘EEE’}, ‘d2’: {‘Gender’: ‘Male’, ‘Student_2’: ‘Rathnam’, ‘Dept’: ‘IT’}, ‘d1’: {‘Gender’: ‘Male’, ‘Dept’: ‘CSE’, ‘Student_1’: ‘Sudhakar’}} |
Deleting elements in a Nested Dictionary
The following example describes how to delete an element in a nested dictionary.
>>> del(n_dict[‘d3’][‘Student_3’]) #Deleting an element
>>> n_dict {‘d3’: {‘Gender’: ‘Male’, ‘Dept’: ‘EEE’}, ‘d2’: {‘Gender’: ‘Male’, ‘Student_2’: ‘Rathnam’, ‘Dept’: ‘IT’}, ‘d1’: {‘Gender’: ‘Male’, ‘Dept’: ‘CSE’, ‘Student_1’: ‘Sudhakar’}} >>> >>> >>> del(n_dict[‘d3’]) #Deleting a dictionary >>> n_dict {‘d2’: {‘Gender’: ‘Male’, ‘Student_2’: ‘Rathnam’, ‘Dept’: ‘IT’}, ‘d1’: {‘Gender’: ‘Male’, ‘Dept’: ‘CSE’, ‘Student_1’: ‘Sudhakar’}} |
Iteration through a Nested Dictionary
We can iterate through every element in a nested dictionary by using for loops. The below example describes the
To print the id’s(values) of dictionaries
Output
Dict_Id: d3
Dict_Id: d2 Dict_Id: d1 |
To print the keys of dictionaries
Output
d1: dict_keys([‘d1’, ‘d2’, ‘d3’])
d2: dict_keys([‘d1’, ‘d2’, ‘d3’]) d3: dict_keys([‘d1’, ‘d2’, ‘d3’]) |
To print the key:value pairs in a dictionary
Output
Dict_Id: d1
Dict_Id: d2 Dict_Id: d3 d1: dict_keys([‘d1’, ‘d2’, ‘d3’]) d2: dict_keys([‘d1’, ‘d2’, ‘d3’]) d3: dict_keys([‘d1’, ‘d2’, ‘d3’]) |