
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
Declare a Multi-Dimensional Dictionary in Python
Multidimensional Dictionaries in python
Multidimensional dictionaries are part of the dictionaries in Python, which are used to store data. Created by assigning a dictionary to a key within another dictionary, these structures are represented by curly braces {} and can accommodate any data type. Their mutable nature allows modifications after creation.
They are composed of unique key-value pairs, separated by a colon (:). While keys are unique, values can be duplicated. Accessing values requires using keys, as dictionaries do not support indexing. To retrieve specific key values, use the key combined with indexing.
Syntax
The following is the syntax for creating multidimensional dictionaries in Python.
variable_name = {k1:{d1},k2:{d2}}
Where,
-
k1,k2 are the keys of the dictionary
-
d1,d2 are the dictionaries with keys and values
Two-Dimensional Dictionary
When we pass dictionaries as the values of the keys, then two-dimensional dictionaries will be generated.
To create the 2-d dictionary, we separately created two dictionaries with the variable names d1 and d2. Next, we created the 2-d dictionary using the created individual dictionaries d1 and d2 as values and 1d, 2d as the keys and assigned the created dictionary to the variable dict_2d. Next, printed the created 2-d dictionary.
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)
Output
The following is the output of the 2-d dictionary created.
Two dimensional dictionary: {'1d': {'a': 10, 'b': 20, 'c': 30}, '2d': {1: 20, 2: 30, 3: 30}}
Three-Dimensional Dictionary
Here, if we want to create a three-dimensional dictionary, then we have to pass the three keys with the values as a dictionary.
Firstly, we created three dictionaries: d1, d2, and d3. Then, we used these dictionaries as values in a new dictionary dict_3d with keys 1d, 2d, and 3d. Finally, we printed the dict_3d.
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)
Output
The following is the output of the 3-d dictionary created.
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
In this example, if we want to create a four-dimensional dictionary, then we have to pass the four keys with the values in dictionary format.
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)
Output
We can observe the 4-d dictionary in the output.
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']}}