Skip to main content

Lesson 3 · Lists

Module 4 · Manhattan Navigation

Lesson 3 · Lists

50 minPython dataStoring a path
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

A tuple stores one position. A path is many positions in order — and for that you need a list. By the end, you'll represent a route as a list of coordinate tuples like [(0,0), (1,0), (2,0), (2,1), (2,2), (2,3)], and build one up step by step — exactly what the Manhattan algorithm does next lesson.

Learning Objectives

By the end of this lesson you will be able to:

  • Create a list with [] and see how it differs from a tuple
  • Add items with append() and count them with len()
  • Loop through a list with a for loop
  • Build a list step by step inside a loop, and store a path as a list of tuples

List vs. tuple

A list is an ordered collection written with square brackets — and unlike a tuple, it's mutable (it can grow and change):

numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # 1 — indexing works like tuples
print(len(numbers)) # 5 — how many items
TupleList
Brackets( )[ ]
Can change?No (immutable)Yes (mutable)
Best fora fixed positiona growing path

So a coordinate is a tuple; a path is a list of tuples.

Knowledge Check

Which is true about lists compared to tuples?

Growing a list with append()

append() adds an item to the end of a list. Start empty and build up:

path = []
path.append((0, 0))
path.append((1, 0))
path.append((2, 0))
print(path) # [(0, 0), (1, 0), (2, 0)]
print(len(path)) # 3

Loop through it with a for loop:

for step in path:
print("Visit:", step, " row:", step[0], " col:", step[1])
Knowledge Check

Given path = [(0, 0), (1, 0), (2, 0)], what does path[2] return?

Knowledge Check

For that same path, what does path[2][1] return?

Activity · Build a path in a loop

The pattern the whole module depends on: start empty, append inside a loop.

path = []
for row in range(5):
path.append((row, 0))
print(path) # [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0)]

Trace it and you'll see the list grow by one each pass. Change the loop to vary the column instead, and you build a horizontal path. Combine a "go down" loop and a "go right" loop and you've basically written the Manhattan algorithm.

The most common list bug

Indentation decides what's inside the loop. Get it wrong and only the last item is added:

# WRONG — append is outside the loop
path = []
for i in range(3):
position = (i, 0)
path.append(position) # only runs once → [(2, 0)]

# CORRECT — append is inside the loop
path = []
for i in range(3):
position = (i, 0)
path.append(position) # runs each pass → [(0, 0), (1, 0), (2, 0)]
Knowledge Check

A student's loop ends with a list holding only the LAST coordinate instead of all of them. What's wrong?

Real-world connections

"A collection you build up and loop over" is one of the most-used ideas in software:

Apps

Feeds & playlists

A playlist or a feed is a list you add to and iterate through, item by item.

Navigation

Route waypoints

A GPS route is a list of waypoints the app walks through in order.

Data

Records & logs

Programs collect results into a list, then loop over them to summarize or display.

Wrap-up

  • Tuple vs list — which is for a position, which for a path? (Tuple = position; list = path.)
  • What does append() do? (Adds an item to the end.)
  • What's the pattern for building a path? (Start with [], append() inside a loop.)

Resources