Nested Dictionaries0%

Nested Dictionaries

Beginner9 min readUpdated: 2026-09-03
Study Materials

Nested Dictionaries

Nested dictionaries contain dictionaries as values, mirroring modern REST API JSON formats.


Key Concepts & Detailed Explanation

undefined


Code Examples & Output

Python
students_db = {
"MSK-101": {
"name": "Pooja Sharma",
"course": "Python for Beginners",
"marks": {"theory": 88, "practical": 94}
},
"MSK-102": {
"name": "Aman Verma",
"course": "Web Development",
"marks": {"theory": 78, "practical": 85}
}
}
 
# Accessing multi-level data
student_name = students_db["MSK-101"]["name"]
practical_score = students_db["MSK-101"]["marks"]["practical"]
 
print(f"Student: {student_name} | Practical Score: {practical_score}")

Expected Output:

Output
Student: Pooja Sharma | Practical Score: 94

Best Practices & Common Pitfalls

Use multiple square brackets (dict[k1][k2][k3]) to navigate deep hierarchies safely.


Practice Quiz

1. How do you access 'city' in 'users = {'u1': {'address': {'city': 'Agra'}}}'?

  • A) users['u1', 'address', 'city']
  • B) users['u1']['address']['city']
  • C) users.u1.address.city
  • D) users['city']
Answer: B Explanation: Chain square brackets for each nested level: users['u1']['address']['city'].

2. Which common web data format directly maps to Python nested dictionaries?

  • A) HTML
  • B) CSV
  • C) JSON
  • D) Markdown
Answer: C Explanation: JSON (JavaScript Object Notation) directly maps to Python nested dictionaries and lists.

Practice Challenge

Create a nested dictionary representing a company with 2 departments, each having 2 employees.

Next Lesson

Project: Student Records

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext