Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define a Python dictionary within dictionary?
A Dictionary in Python is a mutable, unordered collection of data in a key-value format. A dictionary can also contain another dictionary as a value, and this is known as a Nested Dictionary or a dictionary within a dictionary.
In this article, we are going to explore all the different ways to use a dictionary within a dictionary in Python.
Syntax to define a Dictionary within another
Following is the syntax of creating a Dictionary within another Dictionary in Python -
outer_dict = {
'key1': {
'inner_key1': 'value1',
'inner_key2': 'value2'
},
'key2': {
'inner_key1': 'value3',
'inner_key2': 'value4'
}
}
Note: We can access nested dictionary elements using multiple keys as outer_dict['key1']['inner_key1'].
Example-1: Create a basic Dictionary
In this example, we are going to create a basic Dictionary and then see how to create a Dictionary within a Dictionary -
# Creating a Dictionary
mydict = {
'name': 'Riyaansh',
'age': 30,
'department': 'HR'
}
# Displaying the Dictionary
print("Dictionary = ", mydict)
# Length of the Dictionary
print("Dictionary Length = ", len(mydict))
Following is the output of the above program -
Dictionary = {'name': 'Riyaansh', 'age': 30, 'department': 'HR'}
Dictionary Length = 3
Creating a Dictionary within a Dictionary
Here in this example, we will create a dictionary where each key maps to another dictionary that holds employee details -
# Creating a Dictionary within a Dictionary
employee_data = {
'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'},
'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'},
'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'}
}
# Displaying the Dictionary
print("Employee Data = ", employee_data)
# Length of the Outer Dictionary
print("Total Employees = ", len(employee_data))
Below is the output of the above program -
Employee Data = {'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'}, 'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'}, 'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'}}
Total Employees = 3
Accessing a Dictionary within a Dictionary
We can access the inner dictionary by using the keys of the dictionary. Here is an example of accessing the Dictionary within a Dictionary -
# Creating a Dictionary within a Dictionary
employee_data = {
'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'},
'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'},
'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'}
}
# Accessing inner dictionary
print("emp2 Name = ", employee_data['emp2']['name'])
print("emp3 Department = ", employee_data['emp3']['department'])
Below is the output of the above program -
emp2 Name = Anil emp3 Department = Marketing
Adding a New Inner Dictionary
In this example, we will add a new dictionary entry to our existing dictionary using a new key and value pair by using the assignment operator -
# Creating a Dictionary within a Dictionary
employee_data = {
'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'},
'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'},
'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'}
}
# Adding new employee
employee_data['emp4'] = {'name': 'Niharikaa', 'age': 32, 'department': 'Finance'}
# Display the new employee record
print("emp4 Record = ", employee_data['emp4'])
Below is the output of the above program -
emp4 Record = {'name': 'Niharikaa', 'age': 32, 'department': 'Finance'}
Looping through a Dictionary within a Dictionary
In this example, we will iterate over the outer dictionary and then over each inner dictionary to display complete all details of the employee information -
# Creating a Dictionary within a Dictionary
employee_data = {
'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'},
'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'},
'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'},
'emp4': {'name': 'Niharikaa', 'age': 32, 'department': 'Finance'}
}
# Looping through nested dictionaries
for emp_id, emp_info in employee_data.items():
print(f"Employee ID: {emp_id}")
for key, value in emp_info.items():
print(f" {key}: {value}")
Below is the output of the above program -
Employee ID: emp1 name: Riyaansh age: 30 department: HR Employee ID: emp2 name: Anil age: 25 department: Engineering Employee ID: emp3 name: Karthikeya age: 28 department: Marketing Employee ID: emp4 name: Niharikaa age: 32 department: Finance
Access Specific Inner Dictionary Keys
We can access the specific values from the inner dictionary using exact outer key of the dictionary -
employee_data = {
'emp1': {'name': 'Riyaansh', 'age': 30, 'department': 'HR'},
'emp2': {'name': 'Anil', 'age': 25, 'department': 'Engineering'},
'emp3': {'name': 'Karthikeya', 'age': 28, 'department': 'Marketing'},
'emp4': {'name': 'Niharikaa', 'age': 32, 'department': 'Finance'}
}
# Specific access
print("emp1 Age = ", employee_data['emp1']['age'])
print("emp4 Department = ", employee_data['emp4']['department'])
Following is the output of the above program -
emp1 Age = 30 emp4 Department = Finance