Open In App

Python Nested Dictionary

Last Updated : 12 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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

Python Nested Dictionary

Let’s look at how to create, add to, access and delete data in nested dictionaries.

1. Creating a Nested Dictionary

Creating 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)

Output
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)

Output
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'])

Output
Name: Ariana
Grade: A

4. 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)

Output
Updated Nested Dictionary:
{'emp1': {'name': 'John', 'age': 28}}

Related Articles:


Similar Reads