Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Python
About Lesson

Multi-Dimensional Dictionary

A multi-dimensional dictionary in Python is just a dictionary where the values are themselves other dictionaries. This means you can store a lot of information in a structured way, with each key pointing to another dictionary that holds more data. You can keep nesting dictionaries inside each other, creating deeper levels of information.

Example:

Python
# Multi-dimensional dictionary
students = {
    'student1': {
        'name': 'Anil',
        'age': 20,
        'courses': ['Math', 'Physics']
    },
    'student2': {
        'name': 'Rohit',
        'age': 22,
        'courses': ['Biology', 'Chemistry']
    }
}

In this example:

The outer dictionary students has keys ‘student1’ and ‘student2’.

Each key maps to another dictionary containing information about the student, like ‘name’, ‘age’, and ‘courses’

Accessing Values:

To access the values in a multi-dimensional dictionary, you can use nested keys.

Python
# Accessing Anil's name
print(students['student1']['name'])  # Output: Anil

# Accessing Rohit's age
print(students['student2']['age'])  # Output: 22

# Accessing courses of student1
print(students['student1']['courses'])  # Output: ['Math', 'Physics']

Use Cases:

Multi-dimensional dictionaries are useful in situations where:

You need to store complex, structured data.

You want to represent data in a hierarchical form.

You are working with things like user profiles, datasets with multiple categories, or settings that have subcategories.

Modifying a Multi-Dimensional Dictionary:

You can modify values just like you would in a regular dictionary:

Python
# Update age of student1
students['student1']['age'] = 21

# Add a new course to student2
students['student2']['courses'].append('Geography')

# Add a new student
students['student3'] = {
    'name': 'Aman',
    'age': 23,
    'courses': ['History', 'Math']
}

Nested Dictionary with Multiple Levels:

Multi-dimensional dictionaries can be even more complex, with more levels of nesting:

Python
# Example of a 3-level deep dictionary
company = {
    'HR': {
        'employees': {
            'Aman': {'age': 30, 'role': 'Manager'},
            'Paras': {'age': 28, 'role': 'Recruiter'}
        },
        'departments': ['Recruitment', 'Payroll']
    },
    'IT': {
        'employees': {
            'Manish': {'age': 35, 'role': 'Developer'},
            'Seerat': {'age': 40, 'role': 'Systems Administrator'}
        },
        'departments': ['Development', 'Infrastructure']
    }
}

Key Operations:

Add a new nested key-value pair.

Python
students['student3'] = {'name': 'Sam', 'age': 24, 'courses': ['English', 'Math']}

Update a value at a deeper level

Python
students['student1']['courses'].append('Art')

Iterating through nested dictionaries: You can use loops to iterate through the dictionary and its nested dictionaries.

Python
student, info in students.items(): print(f"{student}: {info['name']}, Age: {info['age']}, Courses: {', '.join(info['courses'])}")