✦ Python Foundations Collections module

Concept 13 · Nested Collections

Reach inside collections.
One level at a time.

Apps often store structured data as lists of dictionaries or dictionaries whose values are lists.

01 Learn02 Try03 Predict

Collections can contain other collections.

Apps often store structured data as lists of dictionaries or dictionaries whose values are lists.

student = {"name": "Ana", "scores": [8, 9]}
print(student["scores"][1])

First the key selects the inner list; then index 1 selects its second score.

Try changing one value, then explain which part of the collection changed and why.

↳

Keep this in mind: Read the collection operation before predicting the result.

Try it
students = [
    {"name": "Ana", "scores": [8, 9]},
    {"name": "Marko", "scores": [7, 10]}
]

print(students[0]["name"])
print(students[1]["scores"][0])

Run the example, then change one value and observe the result.

A list of dictionaries
users = [{"name": "Ana"}, {"name": "Marko"}]
print(users[1]["name"])

Use a numeric index to choose a dictionary from the list, then a key to choose its value.

A dictionary of lists
groups = {"python": ["Ana", "Marko"]}
print(groups["python"][0])

Use a key to choose the list, then an index to choose an item inside it.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

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

Ask Sloby about this concept

Describe what feels unclear and Sloby will guide your reasoning.

Hi! I'm Sloby. Let's explore this Python concept together.

What will this program print, and why? Explain the list index, dictionary keys, and inner list length.

projects = [
    {"name": "Website", "tasks": ["Design", "Code"]},
    {"name": "App", "tasks": ["API", "Testing", "Deploy"]}
]

print(projects[1]["name"])
print(projects[0]["tasks"][1])
print(len(projects[1]["tasks"]))
Python AI Tutor · Learn by understanding, not just by running code.