Pylerno
English

Nested data

Trace a path through nested lists and dictionaries one level at a time.

On this page

The concept

Real data often has several levels: a list of students, a dictionary for each student, and a list of scores within that dictionary. At every step, ask what type the current value has and which operation is appropriate next.

A table is often represented by a list of row lists. Each row must be independently created when rows should be independently mutable. Repeating a reference to one row does not create separate copies of its contents. Building the rows with a loop makes their independent construction visible.

Worked example

python
students = [{"name": "Sara", "scores": [80, 90]}, {"name": "Dana", "scores": [70, 85]}]
print(students[0]["scores"][1])
for student in students:
    average = sum(student["scores"]) / len(student["scores"])
    print(student["name"], average)
matrix = []
for _ in range(2):
    matrix.append([0] * 3)
matrix[0][0] = 9
print(matrix)

Trace the result

  1. students[0] is a record; its scores field is a list; index 1 selects the second score.
  2. Each student's average is computed from that student's own score list.
  3. Each matrix row is created separately, so editing the first cell does not alter the second row.
text
90
Sara 85.0
Dana 77.5
[[9, 0, 0], [0, 0, 0]]
Line-by-line walkthrough

Line 1: students = [{"name": "Sara", "scores": [80, 90]}, {"name": "Dana", "scores": [70, 85]}]

Evaluate [{'name': 'Sara', 'scores': [80, 90]}, {'name': 'Dana', 'scores': [70, 85]}], then bind or update students with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 2: print(students[0]["scores"][1])

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 3–5: for student in students:

Obtain each item from students and bind it to student before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Line 4: average = sum(student["scores"]) / len(student["scores"])

Evaluate sum(student['scores']) / len(student['scores']), then bind or update average with the result. Assignment itself does not imply a deep copy.

Line 5: print(student["name"], average)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: matrix = []

Evaluate [], then bind or update matrix with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Lines 7–8: for _ in range(2):

Obtain each item from range(2) and bind it to _ before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Line 8: matrix.append([0] * 3)

Call matrix.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.

Line 9: matrix[0][0] = 9

Evaluate 9, then bind or update matrix[0][0] with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 10: print(matrix)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

[[0] * 3] * 2 repeats the same inner-list reference. Mutating one row can then appear to mutate several rows.

Check your understanding

What does this program print?

python
rows = [{"n": 3}, {"n": 6}]
print(rows[1]["n"])
Choose one answer

Search documentation

Share this page