Unit 3 · Programming 2 · Lesson 3.7

Lists II: Growing and Shrinking

A list isn’t fixed. You can add to it, remove from it, and ask how long it is — and the most useful move of all is building a list up from nothing inside a loop.

Big Idea 3 — Algorithms & ProgrammingFluency day

Before this lesson — read on Runestone

Same chapter as 3.6 — today it’s the methods that change a list. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. In 3.6 a list held a fixed set of values. Today it becomes dynamic — and you meet the pattern where an accumulator (3.4) builds a list instead of a number.

2

The Hook

A shopping cart starts empty. Nobody hands you a finished cart. You start with nothing and add items one at a time — sometimes removing one you changed your mind about. The list grows and shrinks as you go.

Programs do the same: start with an empty list, then append to it inside a loop as data arrives. That “empty list plus a loop that adds” is one of the most common shapes in all of programming.

3

The Idea

A handful of operations change a list. Here are the four you need, in both notations:

Do thisPythonAP Pseudocode
add to the endnums.append(7)APPEND(nums, 7)
insert at a positionnums.insert(0, 7)INSERT(nums, 1, 7)
remove a positionnums.pop(2)REMOVE(nums, 3)
how many elementslen(nums)LENGTH(nums)
len() / LENGTH() is the honest count of elements — and it’s how you avoid the index flip biting you. The last valid Python index is always len(nums) - 1; in AP it’s simply LENGTH(nums). Reach past that and you get an error.

Python — build a list in a loop

nums = []          # start empty
for i in range(3):
    x = int(input("num? "))
    nums.append(x)  # grow it
print(nums)       # all three

What’s happening

# the accumulator — but a list,
# not a number
# read one value per pass
# append = the "update" step
# use the finished list after

Hover or tap a line to light its twin. This is the accumulator pattern from 3.4 with a list in the accumulator’s seat: initialize empty, append inside, use after.

4

Try It — Grow and Shrink

Add values to the end, remove the last one, and watch the length update. This is append, pop, and len in action.

nums = []
len(nums) = 0
Append a few values, then pop one off the end.
5

Vocabulary

append Programs
Add an element to the end of a list (append / APPEND).
length Programs
The number of elements in a list (len() / LENGTH()). The last Python index is len - 1.

No brand-new Big Ideas today — this is a fluency day. The goal is speed and confidence with list operations.

6

Check

Five questions. Track the list step by step as it changes, then pick your answer.

0 of 5 answered
7

Impact Check

Empty list + a loop = most data you’ve ever seen

Every feed you scroll was built this way: start empty, loop over incoming posts, append each one. Every “add to cart,” every uploaded photo joining an album, every new message landing in a thread — an append to a growing list. It’s the shape of software that collects.

And len() is quietly everywhere too: the little number badge on your notifications, “3 items in cart,” “127 unread” — that’s just the length of a list, read out loud.

8

Connections

Came from 3.4 — building a list in a loop is the accumulator pattern with a list in place of the running number.
Next in 3.8 — now that lists can hold anything, you’ll loop over every element with traversal.
Returns in 3.10 — the gradebook reads scores into a list with exactly this “append in a loop” move.
Returns on the exam — APPEND, INSERT, REMOVE and LENGTH in pseudocode are tested directly. Know what each does to the positions around it.
← 3.6 Lists I