Instagram
youtube
Facebook
Twitter

Python Program to Access Value of a Nested Dictionary Key

A Python Program to Access Value of a Nested Dictionary Key?

Code Explanation:

Nested Dictionary:
The variable sample_dict contains a multi-level (nested) dictionary with keys like class, student, and marks.

Accessing Nested Value:
The key 'history' is deeply nested under "class" → "student" → "marks".

Direct Access:
sample_dict["class"]["student"]["marks"]["history"] accesses the value 85 stored under the 'history' key.

Output Display:
The print() function shows the value of 'history' marks on the screen.

 

Program:

sample_dict = {

    "class": {

        "student": {

            "name": "Mike",

            "marks": {

                "math": 90,

                "history": 85

            }

        }

    }

}

print("History Marks:", sample_dict["class"]["student"]["marks"]["history"])