Unit 4 • Lesson 4

Nested Lists and 2D Structures

Overview

Explore lists within lists to represent grids or tables. You'll learn how to access elements in multi-dimensional structures and loop through them efficiently, enabling you to work with complex data arrangements like matrices and nested datasets.

Intermediate 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Nested lists: Understand how to create lists that contain other lists.
  • 2D structures: Learn how nested lists represent grids and tables.
  • Accessing elements: Discover how to access items in nested lists using multiple indices.
  • Looping through nested lists: Learn how to iterate through 2D structures efficiently.

Why This Matters

Nested lists let you represent complex data structures like grids, tables, matrices, or game boards. Instead of having one flat list, you can organize data into rows and columns. This is essential for working with tabular data, game boards, or any situation where you need to organize information in multiple dimensions!

Step 1: What Are Nested Lists?

A nested list is a list that contains other lists as its elements. This creates a two-dimensional (2D) structure, like a grid or table.

Creating Nested Lists
# A nested list representing a 2x3 grid
matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

print(matrix)  # Output: [[1, 2, 3], [4, 5, 6]]
1

Lists Inside Lists

Each element of the outer list is itself a list. This creates a structure where you have rows (the outer list) and columns (the inner lists).

2

2D Structure

Think of it like a table: the outer list represents rows, and each inner list represents the cells in that row. This gives you a two-dimensional structure.

3

Any Size

Nested lists can have any number of rows and columns. Each row can even have a different number of columns, though it's more common to keep them the same size.

Key Concept: A nested list is like a table or grid. The outer list contains rows, and each row (inner list) contains columns. This lets you organize data in two dimensions - rows and columns - just like a spreadsheet!

Mini Practice #1: Creating Nested Lists

Try It Yourself

Try creating a nested list:

Press Run to see output

What happened? You created a nested list where the outer list has 3 elements (rows), and each inner list has 3 elements (columns). When you access grid[0], you get the first row (which is itself a list: [1, 2, 3]). This creates a 3x3 grid structure!

Step 2: Accessing Elements in Nested Lists

To access an element in a nested list, you need two indices: one for the row and one for the column.

Accessing Elements
matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

print(matrix[0][0])  # Output: 1 (first row, first column)
print(matrix[0][1])  # Output: 2 (first row, second column)
print(matrix[1][2])  # Output: 6 (second row, third column)

How It Works

The first index [0] selects the row (the outer list element). The second index [0] selects the column (the element within that row). So matrix[0][1] means "get row 0, column 1" - the second element in the first row!

Step 3: Looping Through Nested Lists

You can loop through nested lists using nested loops - one loop for rows and another for columns:

Nested Loops
matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

# Loop through each row
for row in matrix:
    # Loop through each element in the row
    for element in row:
        print(element, end=" ")
    print()  # New line after each row
1

Outer Loop

The outer loop (for row in matrix) iterates through each row. Each iteration gives you one inner list.

2

Inner Loop

The inner loop (for element in row) iterates through each element in the current row. This lets you access every element in the nested list.

3

Processing

You can process each element individually. The print() after the inner loop creates a new line after each row, making the output look like a grid.

Mini Practice #2: Looping Through Nested Lists

Try It Yourself

Try looping through a nested list:

Press Run to see output

What happened? You used a loop to go through each row in the nested list. Each row represents one student's scores. The enumerate() function gives you both the index and the value, making it easy to number the students. Nested lists are perfect for organizing data like this - multiple items for multiple entities!

Step 4: Modifying Nested Lists

You can modify elements in nested lists just like regular lists:

Modifying Elements
matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

matrix[0][1] = 99  # Change element at row 0, column 1
print(matrix)  # Output: [[1, 99, 3], [4, 5, 6]]

Remember: Use two indices to modify an element: matrix[row][column] = new_value. This changes the element at that specific position in the nested list!

End-of-Lesson Exercises

Exercise 1: Create and Access a Nested List

Create a nested list called grid with 2 rows and 3 columns: [[1, 2, 3], [4, 5, 6]]. Print the element at row 0, column 1, and the element at row 1, column 2.

Use double indexing: grid[row][column] to access elements.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Loop Through a Nested List

Create a nested list called table with 3 rows: [[10, 20], [30, 40], [50, 60]]. Use nested loops to print each element on a separate line.

Use a nested for loop: for row in table, then for element in row.

Write your code above and click "Check Answer" to verify it's correct.