Table of Contents
Introduction to Nested Dictionary in Python
In Python, Dictionary is used to store the data in the form of key-value pair. Many times while working on a real time applications, we may need to store the data in a complex structure. So, a nested dictionary can help us to accomplish this task. When we write a dictionary inside another dictionary, it is referred as a Nested Dictionary. In other words, its a collection of dictionaries into one single dictionary. Although, it works like a ordinary dictionary but the slicing operation is not possible. Moreover, we can shrink or grow nested dictionary as need.
The code snippet below shows the nested dictionary. Here, the nested_dict is a nested dictionary with the dictionary d1 and d2. So, they are two dictionary each having own key and value.
nested_dict = { 'd1': {'key_1': 'value_1'},
'd2': {'key_2': 'value_2'}}
Different Operations on Nested Dictionary
The various operations that can be performed on nested dictionary are as listed below.
- Create a Nested Dictionary
- Access the elements
- Add the element
- Add the dictionary
- Delete the element
- Delete the dictionary
Scenario-1: Create a Nested Dictionary
Here, we are taking dictionary Student containing the records of all the students according to their rollno. The inner dictionary will contain the name, age and department where student is admitted. Under the key rollno, each student will have a key-value pair storing the name, age and department. So, we need to create a nested dictionary for this record structure.
Example :
# Creating a Dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Keya', 'age': '18', 'dept':'Comp'}}
print(Student)
Output
{1: {'name': 'Jim', 'age': '19', 'dept': 'Comp'}, 2: {'name': 'Maria', 'age': '18', 'dept': 'Civil'}, 3: {'name': 'Keya', 'age': '18', 'dept': 'Comp'}}
Scenario-2: Access elements of a Nested Dictionary
To access element of a dictionary, we will use [] and indexing in Python. For example, to access the name and dept of key 1, we will write Student[1]['name'] and Student[1]['dept'].
Example :
# Creating a Dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Keya', 'age': '18', 'dept':'Comp'}}
# Accessing the value of the given key
print(Student[1]['name'])
print(Student[1]['age'])
print(Student[1]['dept'])
Output
Jim
19
Comp
Scenario-3: Add element to a Nested Dictionary
Here, we are adding a new Student to the dictionary. Also we are updating the value of dept key for student 3.
Example :
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Keya', 'age': '18', 'dept':'Comp'}}
# Adding empty dictionary
Student[4] = {}
# Adding the keys and values in inner dictionary
Student[4]['name'] = 'Lisa'
Student[4]['age'] = '20'
Student[4]['dept'] = 'Civil'
# Updating the exisiting content
Student[3]['dept']="EXTC"
print(Student)
Output
{1: {'name': 'Jim', 'age': '19', 'dept': 'Comp'}, 2: {'name': 'Maria', 'age': '18', 'dept': 'Civil'}, 3: {'name': 'Keya', 'age': '18', 'dept': 'EXTC'}, 4: {'name': 'Lisa', 'age': '20', 'dept': 'Civil'}}
Scenario-4: Add Dictionary to the nested dictionary
In this example, we will assign a dictionary literal to Student[4]. The literal have keys name, age and dept with respective values. Thereafter, we will print the Student[4], to see that the dictionary 4 is added in nested dictionary Student.
Example :
# Creating a Dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Lisa', 'age': '20', 'dept':'Civil'}}
# Adding the dictionary to the student dictionary
Student[4] = {'name': 'Peter', 'age': '19', 'dept':'Comp'}
# Printing
print(Student[4])
Output
{'name': 'Peter', 'age': '19', 'dept':'Comp'}
Scenario-5: Delete elements from a Nested Dictionary
Here, we will use "del" statement to delete elements from dictionary.
Example :
# Creating a Dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Lisa', 'age': '20', 'dept':'Civil'},
4: {'name': 'Peter', 'age': '19', 'dept':'Comp'}}
# Delete the given key value pair from the Student dictionary
del Student[3]['dept']
del Student[4]['age']
# Printing to confirm the delete
print(Student[3])
print(Student[4])
Output
{'name': 'Lisa', 'age': '20'}
{'name': 'Peter', 'dept': 'Comp'}
Scenario-6: Delete dictionary from a Nested Dictionary
Here, we will use "del" statement to delete a dictionary from the nested dictionary.
Example :
# Creating a dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Lisa', 'age': '20', 'dept':'Civil'},
4: {'name': 'Peter', 'age': '19', 'dept':'Comp'}}
# Deleting the whole record of a student
del Student[3], Student[4]
# Printing to confirm the delete
print(Student)
Output
{1: {'name': 'Jim', 'age': '19', 'dept':'Comp'}, 2: {'name': 'Maria', 'age': '18', 'dept':'Civil'}}
Scenario-7: Traversing Through a Nested Dictionary
Here, we will iterate through each elements in a dictionary using the for loops. In the example, the first loop will return all the keys in the dictionary Student. It consist of the rollno of each student. We use these rollno to unpack the information of each student. The second loop goes through the information of each student. Then, it returns all of the keys name, age, dept of each student's dictionary. Now, we print the key of the student's information and the value for that key.
Example :
# Creating a dictionary
Student = {1: {'name': 'Jim', 'age': '19', 'dept':'Comp'},
2: {'name': 'Maria', 'age': '18', 'dept':'Civil'},
3: {'name': 'Lisa', 'age': '20', 'dept':'Civil'},
4: {'name': 'Peter', 'age': '19', 'dept':'Comp'}}
# Loop to iterate on the key of Student
for rollno, info in Student.items():
print("\nStudent rollno:", rollno)
# Loop to iterate on the value of key which is also a dictionary
for key in info:
print(key + ':', info[key])
Output
Student rollno: 1
name: Jim
age: 19
dept: Comp
Student rollno: 2
name: Maria
age: 18
dept: Civil
Student rollno: 3
name: Lisa
age: 20
dept: Civil
Student rollno: 4
name: Peter
age: 19
dept: Comp
Summary
The knowledge of Nesting Dictionary is very useful while working on real time applications. In many situations, we will need to store the data in the nested structure like we have structure in C for faster access. In this tutorial, we covered the creation, accessing, insertion and deletion operation on Nested Dictionary. We learned in detail about this with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on Nested Dictionary in Python.
References