Computer >> Computer tutorials >  >> Programming >> Python

How to declare a multi dimensional dictionary in Python?


Let us declare three dictionary objects as below −

>>> d1={"name":"Ravi","age":25, "marks":60}
>>> d2={"name":"Anil","age":23, "marks":75}
>>> d3={"name":"Asha", "age":20, "marks":70}

Now we shall use them as values in a parent dictionary

>>>twodimdict={1:d1,2:d2,3:d3}
>>>twodimdict
{1: {'name': 'Ravi', 'age': 25, 'marks': 60}, 2: {'name': 'Anil', 'age': 23, 'marks': 75}, 3: {'name': 'Asha', 'age': 20, 'marks': 70}}

Here d1, d2 and d3 are assigned as value to keys 1,2,3

twodimdict [1] returns d1. Value of a key inside d1 can be obtained as below −

>>>twodimdict[1]
{'name': 'Ravi', 'age': 25, 'marks': 60}
>>>twodimdict[1]["age"]
25

Note that the dictionary items are not indexed.