Lesson 17 of 30

Lesson 17 ? Nested lists and grids

Title: Make tiny maps with lists inside lists

Description: Use nested lists to build rows, columns, and simple game boards.

Why it matters for kids: Many games use grids: tic-tac-toe, mazes, maps, and puzzles.


1. A row of tiles

pythonpython
row = ["grass", "grass", "water"]

print(row[0])
print(row[2])

2. A tiny map

pythonpython
world = [
    ["grass", "tree", "grass"],
    ["water", "bridge", "water"],
    ["grass", "house", "grass"],
]

print(world[1][1])

world[1][1] means row 1, column 1.


3. Print each row

pythonpython
world = [
    [".", ".", "T"],
    ["~", "B", "~"],
    [".", "H", "."],
]

for row in world:
    print(row)

4. Pretty grid

pythonpython
world = [
    [".", ".", "T"],
    ["~", "B", "~"],
    [".", "H", "."],
]

for row in world:
    print(" ".join(row))

5. Easy examples

pythonpython
board = [
    ["X", "O", "X"],
    ["O", "X", "O"],
    ["O", "X", "X"],
]

print(board[0][0])
pythonpython
seats = [
    ["A1", "A2"],
    ["B1", "B2"],
]

print(seats[1][0])
pythonpython
pixels = [
    ["red", "red"],
    ["blue", "blue"],
]

print(pixels)

6. Mini challenge

Click each example to open it.

Example 1

Create one row with three tiles.

Example 2

Print the first tile.

Example 3

Create a 2 by 2 grid.

Example 4

Print row 0 column 0.

Example 5

Create a 3 by 3 map.

Example 6

Put X in one place.

Example 7

Print every row.

Example 8

Join one row with spaces.

Example 9

Make a tiny tic-tac-toe board.

Example 10

Make a tiny classroom seating grid.