In the previous lesson, you learned how to create dictionaries using dictionary comprehension. While a standard dictionary stores a single level of key-value pairs, many real-world applications require storing more complex and structured information. For example, a school may store multiple students, each with their own name, marks, course, and city. Python supports this type of data organization through nested dictionaries.
A nested dictionary is simply a dictionary whose values are themselves dictionaries. This allows you to organize related information in a hierarchical structure, making it easier to represent real-world data such as employee records, product catalogs, customer profiles, JSON data, and API responses.
In this lesson, you will learn what nested dictionaries are, why they are useful, how to create them, access nested values, understand their characteristics, and explore practical examples from real-world programming.
After completing this lesson, you will be able to:
A nested dictionary is a dictionary in which one or more values are themselves dictionaries. Each inner dictionary contains its own set of key-value pairs.
Nested dictionaries are useful when storing structured or hierarchical data because they allow multiple related records to be grouped together.
Nested dictionaries make it possible to organize complex information without creating multiple separate variables.
Common applications include:
A nested dictionary is created by placing dictionaries inside another dictionary.
students = {
"student1": {
"name": "Rahul",
"course": "BCA",
"marks": 92
},
"student2": {
"name": "Priya",
"course": "BSc",
"marks": 88
}
}
print(students)
Output
{
'student1': {
'name': 'Rahul',
'course': 'BCA',
'marks': 92
},
'student2': {
'name': 'Priya',
'course': 'BSc',
'marks': 88
}
}
Each student is represented by an inner dictionary containing personal information.
products = {
"P101": {
"name": "Laptop",
"price": 55000,
"stock": 20
},
"P102": {
"name": "Mouse",
"price": 800,
"stock": 100
}
}
print(products)
Output
{
'P101': {
'name': 'Laptop',
'price': 55000,
'stock': 20
},
'P102': {
'name': 'Mouse',
'price': 800,
'stock': 100
}
}
To access a value inside a nested dictionary, use multiple keys.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
print(students["student1"]["name"])
print(students["student1"]["marks"])
Output
Rahul
92
Python first accesses the outer dictionary and then retrieves the required value from the inner dictionary.
employees = {
"E101": {
"name": "Neha",
"department": "HR",
"salary": 50000
},
"E102": {
"name": "Amit",
"department": "IT",
"salary": 65000
}
}
print(employees["E102"]["department"])
Output
IT
customers = {
"C001": {
"name": "Riya",
"city": "Delhi"
},
"C002": {
"name": "Arjun",
"city": "Mumbai"
}
}
print(customers["C001"]["city"])
Output
Delhi
Consider the following program.
student = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
print(student["student1"]["marks"])
Execution Steps
student1.student1 accesses the inner dictionary.marks retrieves the required value.Output
92
In this section, you learned what nested dictionaries are, why they are useful, how to create them, access nested values, and represent structured information efficiently. You also explored real-world examples, execution flow, and common beginner mistakes. In the next section, you will learn how to add, update, and remove data from nested dictionaries, iterate through nested structures, perform membership testing, and apply these techniques in practical Python programs.
In the previous section, you learned how to create nested dictionaries and access values using multiple keys. In real-world applications, nested dictionaries are rarely static. You often need to add new records, update existing information, remove data, and iterate through multiple levels of nested dictionaries. Python makes these operations straightforward using the same dictionary techniques that you already know.
In this section, you will learn how to add, update, and remove data from nested dictionaries, iterate through nested structures, perform membership testing, and explore practical examples from real-world applications.
Nested dictionaries can be modified just like normal dictionaries. The only difference is that you first access the appropriate inner dictionary before performing the required operation.
You can add an entirely new nested dictionary by assigning a new key to the outer dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
students["student2"] = {
"name": "Priya",
"marks": 88
}
print(students)
Output
{
'student1': {'name': 'Rahul', 'marks': 92},
'student2': {'name': 'Priya', 'marks': 88}
}
You can also add new information to an existing inner dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
students["student1"]["city"] = "Dehradun"
print(students)
Output
{
'student1': {
'name': 'Rahul',
'marks': 92,
'city': 'Dehradun'
}
}
Updating a value requires accessing both the outer key and the inner key.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
students["student1"]["marks"] = 95
print(students)
Output
{
'student1': {
'name': 'Rahul',
'marks': 95
}
}
The del statement removes a specific key from an inner dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92,
"city": "Dehradun"
}
}
del students["student1"]["city"]
print(students)
Output
{
'student1': {
'name': 'Rahul',
'marks': 92
}
}
Removing the outer key deletes the complete nested dictionary.
students = {
"student1": {
"name": "Rahul"
},
"student2": {
"name": "Priya"
}
}
del students["student2"]
print(students)
Output
{
'student1': {
'name': 'Rahul'
}
}
You can use nested for loops to traverse every level of a nested dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92
},
"student2": {
"name": "Priya",
"marks": 88
}
}
for student_id, details in students.items():
print(student_id)
for key, value in details.items():
print(key, ":", value)
Output
student1
name : Rahul
marks : 92
student2
name : Priya
marks : 88
The in operator checks whether a key exists in either the outer or inner dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
print("student1" in students)
print("marks" in students["student1"])
Output
True
True
employees = {
"E101": {
"name": "Neha",
"salary": 50000
}
}
employees["E101"]["salary"] = 55000
print(employees)
Output
{
'E101': {
'name': 'Neha',
'salary': 55000
}
}
products = {
"P101": {
"name": "Laptop",
"stock": 20
}
}
products["P101"]["stock"] = 18
print(products)
Output
{
'P101': {
'name': 'Laptop',
'stock': 18
}
}
customers = {
"C001": {
"name": "Riya",
"city": "Delhi"
}
}
customers["C001"]["phone"] = "9876543210"
print(customers)
Output
{
'C001': {
'name': 'Riya',
'city': 'Delhi',
'phone': '9876543210'
}
}
Consider the following program.
students = {
"student1": {
"marks": 92
}
}
students["student1"]["marks"] = 95
print(students)
Execution Steps
student1.marks is updated.Output
{
'student1': {
'marks': 95
}
}
In this section, you learned how to add, update, and remove data from nested dictionaries, iterate through multiple levels using nested loops, perform membership testing, and apply these techniques in practical programs. You also explored execution flow and common beginner mistakes. In the next section, you will learn advanced techniques for working with nested dictionaries, including copying nested dictionaries, combining nested data, best practices, performance tips, and real-world applications.
In the previous section, you learned how to add, update, remove, and iterate through nested dictionaries. As applications become larger, nested dictionaries often contain hundreds or thousands of records. Python provides several techniques for managing these complex data structures efficiently while keeping your code readable and maintainable.
In this section, you will learn how to combine nested dictionaries, create copies, understand shallow and deep copying, explore best practices, improve performance, and apply nested dictionaries in real-world applications.
Nested dictionaries support most of the operations available for normal dictionaries. However, because they contain multiple levels, you should understand how copying and combining nested data works.
You can combine multiple nested dictionaries using the update() method.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
new_students = {
"student2": {
"name": "Priya",
"marks": 88
}
}
students.update(new_students)
print(students)
Output
{
'student1': {
'name': 'Rahul',
'marks': 92
},
'student2': {
'name': 'Priya',
'marks': 88
}
}
The copy() method creates a shallow copy of a nested dictionary.
students = {
"student1": {
"name": "Rahul",
"marks": 92
}
}
backup = students.copy()
print(backup)
Output
{
'student1': {
'name': 'Rahul',
'marks': 92
}
}
A shallow copy duplicates only the outer dictionary. The inner dictionaries are still shared.
students = {
"student1": {
"marks": 92
}
}
backup = students.copy()
backup["student1"]["marks"] = 95
print(students)
print(backup)
Output
{
'student1': {
'marks': 95
}
}
{
'student1': {
'marks': 95
}
}
Both dictionaries change because they reference the same inner dictionary.
Use the deepcopy() function from the copy module when you need a completely independent copy.
from copy import deepcopy
students = {
"student1": {
"marks": 92
}
}
backup = deepcopy(students)
backup["student1"]["marks"] = 95
print(students)
print(backup)
Output
{
'student1': {
'marks': 92
}
}
{
'student1': {
'marks': 95
}
}
A deep copy duplicates every level of the nested dictionary.
school = {
"Class10": {
"Students": 45,
"Teacher": "Anita"
},
"Class12": {
"Students": 38,
"Teacher": "Rajesh"
}
}
print(school)
Output
{
'Class10': {
'Students': 45,
'Teacher': 'Anita'
},
'Class12': {
'Students': 38,
'Teacher': 'Rajesh'
}
}
inventory = {
"Laptop": {
"Price": 55000,
"Stock": 20
},
"Mouse": {
"Price": 800,
"Stock": 120
}
}
print(inventory)
Output
{
'Laptop': {
'Price': 55000,
'Stock': 20
},
'Mouse': {
'Price': 800,
'Stock': 120
}
}
employees = {
"E101": {
"Name": "Neha",
"Department": "HR"
},
"E102": {
"Name": "Rahul",
"Department": "IT"
}
}
print(employees)
Output
{
'E101': {
'Name': 'Neha',
'Department': 'HR'
},
'E102': {
'Name': 'Rahul',
'Department': 'IT'
}
}
deepcopy() when creating independent copies.items() when iterating through nested dictionaries.deepcopy() only when required because it consumes additional memory.Consider the following program.
students = {
"student1": {
"marks": 92
}
}
backup = students.copy()
print(backup)
Execution Steps
copy() method creates a shallow copy of the outer dictionary.Output
{
'student1': {
'marks': 92
}
}
copy() creates a deep copy.In this section, you learned how to combine nested dictionaries, create shallow and deep copies, understand the difference between copy() and deepcopy(), apply best practices, improve performance, and use nested dictionaries in real-world applications. In the final section, you will review the complete lesson through a lesson summary, key takeaways, FAQs, coding exercises, interview questions, a mini project, and a preview of the next lesson on Dictionary vs List vs Tuple vs Set: Complete Guide for Beginners.
In this lesson, you learned how to use nested dictionaries in Python to organize and manage hierarchical data. A nested dictionary is a dictionary that contains one or more dictionaries as its values, making it ideal for storing structured information such as student records, employee databases, product catalogs, customer profiles, and JSON data.
You began by understanding what nested dictionaries are, why they are useful, and how to create them. You learned how to access nested values using multiple keys and explored several real-world examples.
Next, you learned how to add new records, insert new fields, update existing values, remove nested data, and iterate through nested dictionaries using nested for loops. You also learned how to perform membership testing with nested dictionaries.
Finally, you explored advanced operations such as combining nested dictionaries, creating shallow and deep copies, and learned the differences between copy() and deepcopy(). You also reviewed best practices, performance tips, and common beginner mistakes.
Nested dictionaries are one of the most commonly used data structures in Python because they closely represent real-world hierarchical data and are widely used in APIs, JSON files, web applications, automation, machine learning, and data analysis.
for loops to iterate through nested dictionaries.update() to combine nested dictionaries.copy() creates a shallow copy.deepcopy() creates a completely independent copy.A nested dictionary is a dictionary that contains one or more dictionaries as its values.
They help organize structured and hierarchical information efficiently.
Use multiple dictionary keys, such as students["student1"]["marks"].
Yes. You can add, update, and remove nested data.
Use nested for loops with the items() method.
copy() and deepcopy()?copy() creates a shallow copy, while deepcopy() copies every nested level independently.
Yes. Inner dictionaries can contain lists, tuples, sets, and other data types.
Yes. However, excessive nesting can reduce readability and increase complexity.
They are commonly used in JSON data, APIs, databases, configuration files, and management systems.
They allow complex real-world data to be stored in a clear and organized structure.
city to an existing student.for loops.deepcopy() and compare the results.Create a Python program that stores and manages school records using nested dictionaries.
Your program should:
deepcopy().========== SCHOOL MANAGEMENT SYSTEM ==========
Student Records
{
'student1': {
'name': 'Rahul',
'class': 'BCA',
'marks': 92,
'city': 'Dehradun'
},
'student2': {
'name': 'Priya',
'class': 'BSc',
'marks': 88,
'city': 'Delhi'
}
}
After Updating Marks
Rahul -> 95
After Adding Student
student3 Added Successfully
Displaying All Records
student1
name : Rahul
class : BCA
marks : 95
city : Dehradun
student2
name : Priya
class : BSc
marks : 88
city : Delhi
student3
name : Amit
class : BCom
marks : 90
city : Jaipur
Backup Created Successfully
==============================================
Congratulations! You have successfully learned how to create, manage, and work with nested dictionaries in Python. You can now organize hierarchical data, update nested records, iterate through multiple dictionary levels, and create safe copies using deepcopy().
In the next lesson, you will learn Dictionary vs List vs Tuple vs Set: Complete Guide for Beginners. You will compare Python’s four major collection data types, understand their differences, advantages, limitations, performance characteristics, and learn when to use each one in real-world programming.