Read nested data from the outside in, one level at a time.
Lists select values by position and dictionaries select values by meaningful keys. A nested collection stores another collection as one of its values, which helps programs organize students and scores, courses and lessons, or projects and tasks.
student = {"name": "Ana", "scores": [8, 9]}
print(student["scores"][1])Do not read a long expression all at once. students[0] selects the first dictionary; students[0]["scores"] selects its scores list; students[0]["scores"][1] selects one value inside that list.
Follow the path: students → list; students[0] → first dictionary; students[0]["scores"] → [8, 9]; then index 1 → 9. If the current value is a dictionary, use a key; if it is a list, use an index.
Nested values can change too: students[0]["scores"][1] = 10 changes one value deep in the structure. The outer list, dictionary, and scores list remain the same collections.
course = {"name": "Python Foundations", "student": {"name": "Ana", "scores": [8, 10, 9]}}
print(course["student"]["scores"][1])First select "student", then the inner "scores" list, then index 1. The same outside-in strategy works at every level.
for student in students:
print(student["name"])The loop gives one student dictionary per iteration, so student["name"] uses a key in that current dictionary.
projects[1]["tasks"][2]projects[1] selects the second dictionary; ["tasks"] selects its list; [2] selects "Deploy".
Follow the data one level at a time
- Identify the current valueAsk what value and collection type you have now.
- Choose a key or an indexUse a key for a dictionary and an index for a list.
- Use the returned value for the next stepEach access returns the next value in the path.
Common beginner mistakes
- Using a key on a list
students["name"]fails because students is a list; select a student with an index first. - Using an index on a dictionary
student[0]fails because a dictionary needs a key. - Skipping a levelFollow each returned collection before trying to select its inner value.
- Using a wrong index or keyAn index outside a list can raise
IndexError; a missing dictionary key can raiseKeyError.
