✦ Python Foundations Collections module

Concept 13 · Nested Collections

Reach inside collections.
One level at a time.

Nested collections store one collection inside another. Read the data from the outside in, one level at a time.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

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.

A dictionary can contain another dictionary
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.

A loop can select one dictionary at a time
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.

Trace a project path
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

  1. Identify the current valueAsk what value and collection type you have now.
  2. Choose a key or an indexUse a key for a dictionary and an index for a list.
  3. Use the returned value for the next stepEach access returns the next value in the path.

Common beginner mistakes

  • Using a key on a liststudents["name"] fails because students is a list; select a student with an index first.
  • Using an index on a dictionarystudent[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 raise KeyError.

Python Playground

Loading Python runtime…

Predict the result, then follow each access level by level. Change one index, key, or nested value and run the code again.

nested_collections.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

courses = {
    "python": ["Ana", "Marko"],
    "django": ["Jelena"]
}

print(courses["python"][1])
print(len(courses["django"]))
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.