Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to declare a multi dimensional dictionary in Python?
Multidimensional dictionaries in Python are nested dictionary structures where values can themselves be dictionaries. They are created by assigning a dictionary to a key within another dictionary, represented by curly braces {} and can accommodate any data type.
These structures consist of unique key-value pairs separated by a colon (:). While keys must be unique, values can be duplicated. Since dictionaries don't support indexing, you access values using their keys.
Syntax
The basic syntax for creating multidimensional dictionaries in Python is ?
variable_name = {k1: {d1}, k2: {d2}}
Where:
k1, k2 are the keys of the outer dictionary
d1, d2 are the inner dictionaries with their own keys and values
Two-Dimensional Dictionary
A two-dimensional dictionary contains dictionaries as values. Here's how to create one ?
d1 = {"a": 10, "b": 20, "c": 30}
d2 = {1: 20, 2: 30, 3: 30}
dict_2d = {"1d": d1, "2d": d2}
print("Two dimensional dictionary:", dict_2d)
Two dimensional dictionary: {'1d': {'a': 10, 'b': 20, 'c': 30}, '2d': {1: 20, 2: 30, 3: 30}}
Three-Dimensional Dictionary
To create a three-dimensional dictionary, we add more nested dictionary levels ?
d1 = {"a": 10, "b": 20, "c": 30}
d2 = {1: 20, 2: 30, 3: 30}
d3 = {"language": "python", "library": ["numpy", "pandas"]}
dict_3d = {"1d": d1, "2d": d2, "3d": d3}
print("Three dimensional dictionary:", dict_3d)
Three dimensional dictionary: {'1d': {'a': 10, 'b': 20, 'c': 30}, '2d': {1: 20, 2: 30, 3: 30}, '3d': {'language': 'python', 'library': ['numpy', 'pandas']}}
Four-Dimensional Dictionary
For a four-dimensional dictionary, we continue adding more nested levels ?
d1 = {"a": 10, "b": 20, "c": 30}
d2 = {3: "three", 4: "four", 5: "five"}
d3 = {1: 20, 2: 30, 3: 30}
d4 = {"language": "python", "library": ["numpy", "pandas"]}
dict_4d = {"1d": d1, "2d": d2, "3d": d3, "4d": d4}
print("Four dimensional dictionary:", dict_4d)
Four dimensional dictionary: {'1d': {'a': 10, 'b': 20, 'c': 30}, '2d': {3: 'three', 4: 'four', 5: 'five'}, '3d': {1: 20, 2: 30, 3: 30}, '4d': {'language': 'python', 'library': ['numpy', 'pandas']}}
Accessing Multidimensional Dictionary Values
To access values in nested dictionaries, chain the keys together ?
student_data = {
"student1": {"name": "Alice", "grades": {"math": 85, "science": 92}},
"student2": {"name": "Bob", "grades": {"math": 78, "science": 88}}
}
# Accessing nested values
print("Student 1 name:", student_data["student1"]["name"])
print("Student 1 math grade:", student_data["student1"]["grades"]["math"])
Student 1 name: Alice Student 1 math grade: 85
Conclusion
Multidimensional dictionaries provide a powerful way to organize hierarchical data in Python. They're created by nesting dictionaries as values and accessed using chained key notation. These structures are ideal for representing complex data relationships like student records, configuration settings, or nested JSON-like data.
---