Pilot · L05 · 60 minutes
Lists and data abstraction
Explain the concrete benefit of storing club capacities in one list, read and change lists using AP index 1 compared with Python index 0, and trace filters that keep duplicates and return an empty list when nothing matches.
Topics: 3.2 Data Abstraction · 3.10 Lists
Goals and vocabulary
By the end you can:
- Explain how a list reduces complexity compared with separate variables, with a concrete example of growth.
- Read an item by index in AP pseudocode and in Python.
- Trace APPEND, INSERT, REMOVE and LENGTH, including how indexes shift.
- Trace a filter and a count over a list, including duplicates, no matches and an empty list.
- Recognise when a collection does not store what the program’s purpose requires.
- List
- An ordered collection of values stored under one name.
- Element
- One value in a list.
- Index
- The position of an element. AP pseudocode starts at 1; Python starts at 0.
- Traversal
- Visiting each element of a list in turn, for example with FOR EACH.
- Filter
- Building a new list containing only the elements that meet a condition.
- Data abstraction
- Using one name for a collection of data so programs can work with it without handling each value separately.
Why one list beats many variables
Suppose the club finder stores the places left in each club. With separate variables you write cap1 ← 0, cap2 ← 5, cap3 ← 2, cap4 ← 8, and every calculation names each variable. Adding a fifth club means editing every one of those calculations.
A list is a single named collection of values in order: caps ← [0, 5, 2, 8]. Code that traverses the list works for any number of clubs without changes. This is data abstraction: one name stands for the whole collection, so the program can be written without knowing how many items there will be.
| Approach | Change needed | Risk |
|---|---|---|
| Separate variables | 300 new variables and every calculation rewritten | Easy to miss one; code grows with the data. |
| One list with a traversal | Add the values to the list | Code is unchanged; it handles any length. |
Indexes: AP starts at 1, Python at 0
Each item has an index, its position in the list. In AP CSP pseudocode the first item is at index 1. In Python the first item is at index 0. The same list, read with the same number, can give different items in the two languages.
| Item | AP index | Python index |
|---|---|---|
| "Art" | 1 | 0 |
| "Chess" | 2 | 1 |
| "Code" | 3 | 2 |
So clubs[1] is "Art" in AP pseudocode but "Chess" in Python. Reading an index outside 1 to LENGTH(list) in AP pseudocode is an error.
Changing a list
| Statement | Effect | caps afterwards |
|---|---|---|
| APPEND(caps, 9) | Adds 9 to the end; length grows by 1. | [4, 0, 6, 9] |
| INSERT(caps, 2, 7) | Shifts items at index 2 and after to the right, then puts 7 at index 2. | [4, 7, 0, 6] |
| REMOVE(caps, 1) | Removes the item at index 1; later items shift left; length shrinks by 1. | [0, 6] |
| LENGTH(caps) | Gives the number of items. Does not change the list. | [4, 0, 6] (3 items) |
After an INSERT or REMOVE, the items after that point have new indexes. Trace the whole list after each change rather than remembering positions.
caps.append(9)caps.insert(1, 7) # Python index 1 is AP index 2caps.pop(0) # Python index 0 is AP index 1len(caps)Worked example: filtering and counting
Worked example
Predict the result before reading the trace: keep the clubs with at least 3 places.
caps ← [0, 5, 2, 8]result ← []FOR EACH c IN caps{ IF (c ≥ 3) { APPEND(result, c) }}DISPLAY(result)| c | c ≥ 3? | result |
|---|---|---|
| 0 | false | [] |
| 5 | true | [5] |
| 2 | false | [5] |
| 8 | true | [5, 8] |
Displays [5, 8]. Three cases to check with any filter: duplicates — [3, 3] with minimum 3 keeps both 3s, because each item is tested separately; no matches — minimum 9 gives [], an empty list, not an error; an empty input — [] gives [] because the loop body never runs.
caps = [0, 5, 2, 8]result = []for c in caps: if c >= 3: result.append(c)print(result)Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- List order and duplicates
- Generic traversal versus separate variables
- Preserving associations
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Collection size (0–8)3Waiting
- RepresentationlistWaiting
Ready to explore: Grow a collection without rewriting the algorithm
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 12,7,3,7,0Waiting
- Input 25Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try repeated boundary values, no matches and an empty list.
Common misconceptions
AP and Python indexes match
The belief: clubs[1] is the first club in both languages.
What is actually true: AP pseudocode starts at 1; Python starts at 0. clubs[1] is the first club in AP but the second in Python.
Check yourself: Which language is this code written in, and where does its index start?
Filters remove duplicates
The belief: Filtering [3, 3] keeps only one 3.
What is actually true: Each element is tested separately. Both 3s meet the condition and both are kept.
Check yourself: How many times did the loop body run, and how many times did APPEND run?
Using a list explains abstraction
The belief: “It is simpler because it uses a list.”
What is actually true: Say what is simpler: the same code handles any number of items, so it does not change as the data grows.
Check yourself: What would you have to change if there were 300 items instead of 4?
Guided practice
Guided: trace list changes in both languages
With a partner, predict the list after each line, then trace it. Translate the indexes into Python as you go.
rooms ← ["A12", "B4", "C7"]APPEND(rooms, "D1")INSERT(rooms, 1, "Lab")REMOVE(rooms, 3)DISPLAY(rooms[2])DISPLAY(LENGTH(rooms))- Write rooms in full after each of lines 2–4.
- State what lines 5 and 6 display.
- Write the Python statement that displays the same element as line 5.
- Explain why rooms[2] after line 4 is not the same room as rooms[2] on line 1.
Independent practice
Independent: filter and explain growth
The club finder needs every capacity of at least a chosen minimum.
caps ← [6, 0, 3, 3, 10]minimum ← 3result ← []FOR EACH c IN caps{ IF (c ≥ minimum) { APPEND(result, c) }}- Trace the starter and give result.
- Give result for minimum 11 and for caps ← [].
- Count how many clubs have at least 3 places, and explain why duplicates count separately.
- Describe what a variable-based version (cap1 to cap5) would need if 200 clubs were added, compared with this list version.
Exit check
- For scores ← [7, 2, 9], what is scores[2] in AP pseudocode and in Python?
- Describe how adding 50 items changes a list-based solution and a variable-based solution.
Check yourself
Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.
Lesson practice
Your practice so far
Check an answer to see its rationale.
Apply it somewhere new
Your practice so far
Check an answer to see its rationale.