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 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)
OutputStudent 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)
OutputUpdated 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'])
OutputName: 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)
OutputUpdated Nested Dictionary:
{'emp1': {'name': 'John', 'age': 28}}
Related Articles:
Similar Reads
Python Dictionary Methods Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
5 min read
Python Dictionary get() Method Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).Python Dictionary get() Method Syntax:Syntax : Dict.get(key, Value)Parameters: key: The key name of the item you want to return
3 min read
Python Dictionary Exercise Basic Dictionary ProgramsPython | Sort Python Dictionaries by Key or ValueHandling missing keys in Python dictionariesPython dictionary with keys having multiple inputsPython program to find the sum of all items in a dictionaryPython program to find the size of a DictionaryWays to sort list of dicti
3 min read
Python Dictionary keys() method keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge
2 min read
Python Dictionary items() method items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
2 min read
Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
5 min read