Last Updated : 12 Jul, 2025
A nested dictionary is a dictionary that contains another dictionary as a value. It helps organize complex or grouped data, like student details or product info in a clean and structured way.
Example:
Python
# Creating a Nested Dictionary
Dict = { 1: 'Geeks',
2: 'For',
3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'} }
Illustration using Image
Let’s look at how to create, add to, access and delete data in nested dictionaries.
1. Creating a Nested DictionaryCreating a Nested Dictionary means placing dictionaries as values inside an outer dictionary using curly braces {}.
Example:
This example creates a nested dictionary to store details of multiple students. Each student is added as a key and their information (name, age, grade) is stored in an inner dictionary.
Python
students = {}
students['student1'] = {'name': 'Drake', 'age': 20, 'grade': 'A'}
students['student2'] = {'name': 'Travis', 'age': 22, 'grade': 'B'}
students['student3'] = {'name': 'Charlie', 'age': 21, 'grade': 'A+'}
print("Student Details:")
print(students)
Student Details: {'student1': {'name': 'Drake', 'age': 20, 'grade': 'A'}, 'student2': {'name': 'Travis', 'age': 22, 'grade': 'B'}, 'student3': {'name': 'Charlie', 'age': 21, 'grade': 'A+'}}2. Adding Elements to a Nested Dictionary
Adding elements to a nested dictionary means inserting new key-value pairs into inner dictionaries or adding new inner dictionaries using normal assignment.
Example:
In this Example, a new key (department) is added to an existing inner dictionary and also creates a new inner dictionary for another employee.
Python
person = {'employee1': {'name': 'Janice', 'age': 25}}
# Adding a new key-value pair to existing inner dictionary
person['employee1']['department'] = 'HR'
# Adding a new inner dictionary
person['employee2'] = {'name': 'Jake', 'age': 30, 'department': 'IT'}
print("Updated Nested Dictionary:")
print(person)
Updated Nested Dictionary: {'employee1': {'name': 'Janice', 'age': 25, 'department': 'HR'}, 'employee2': {'name': 'Jake', 'age': 30, 'department': 'IT'}}3. Accessing Elements in a Nested Dictionary
Accessing elements in a nested dictionary means using outer and inner keys to retrieve specific values from structured data.
Example:
This program shows how to access specific values from a nested dictionary by using outer and inner keys.
Python
student = {'student1': {'name': 'Ariana', 'age': 20, 'grade': 'A'}}
# Accessing elements
print("Name:", student['student1']['name'])
print("Grade:", student['student1']['grade'])
Name: Ariana Grade: A4. Deleting from a Nested Dictionary
Deleting from a Nested Dictionary means removing items from a nested dictionary using del or .pop(), either from inner dictionary or whole entry.
Example:
This program demonstrates how to delete data from a nested dictionary. It removes a specific key (dept) from one inner dictionary and deletes an entire inner dictionary (emp2).
Python
employee = {'emp1': {'name': 'John', 'age': 28, 'dept': 'Sales'},
'emp2': {'name': 'Sara', 'age': 32, 'dept': 'HR'} }
# Deleting a key from inner dictionary
del employee['emp1']['dept']
# Deleting an entire inner dictionary
del employee['emp2']
print("Updated Nested Dictionary:")
print(employee)
Updated Nested Dictionary: {'emp1': {'name': 'John', 'age': 28}}
Related Articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4