Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

A Python dictionary is said to have a nested structure if value of one or more keys is another dictionary. A nested dictionary is usually employed to store a complex data structure.

The following code snippet represents a nested dictionary:

marklist = { "Mahesh" : {"Phy" : 60, "maths" : 70}, "Madhavi" : {"phy" : 75, "maths" : 68}, "Mitchell" : {"phy" : 67, "maths" : 71} }

Example 1

You can also constitute a for loop to traverse nested dictionary, as in the previous section.

 
marklist = { "Mahesh" : {"Phy" : 60, "maths" : 70}, "Madhavi" : {"phy" : 75, "maths" : 68}, "Mitchell" : {"phy" : 67, "maths" : 71} } for k,v in marklist.items(): print (k, ":", v)

It will produce the following output −

Mahesh : {'Phy': 60, 'maths': 70}
Madhavi : {'phy': 75, 'maths': 68}
Mitchell : {'phy': 67, 'maths': 71}

Example 2

It is possible to access value from an inner dictionary with [] notation or get() method.

print (marklist.get("Madhavi")['maths']) obj=marklist['Mahesh'] print (obj.get('Phy')) print (marklist['Mitchell'].get('maths'))

It will produce the following output −

68
60
71

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.
Помог ли вам данный ответ? 1 Пользователи нашли это полезным (1 голосов)