Skip to main content

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.

What happens when the school adds 300 clubs
ApproachChange neededRisk
Separate variables300 new variables and every calculation rewrittenEasy to miss one; code grows with the data.
One list with a traversalAdd the values to the listCode 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.

clubs ← ["Art", "Chess", "Code"]
ItemAP indexPython index
"Art"10
"Chess"21
"Code"32

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

AP list procedures, starting from caps ← [4, 0, 6]
StatementEffectcaps 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.

The same operations in Python
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.

AP CSP pseudocode
caps ← [0, 5, 2, 8]result ← []FOR EACH c IN caps{  IF (c ≥ 3)  {    APPEND(result, c)  }}DISPLAY(result)
Trace
cc ≥ 3?result
0false[]
5true[5]
2false[5]
8true[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.

The same filter in Python
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
Open this example on its own

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

Ready to explore: Grow a collection without rewriting the algorithm
  1. Collection size (0–8)3Waiting
  2. 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

Ready to experiment
  1. Input 12,7,3,7,0Waiting
  2. 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.

AP CSP pseudocode
rooms ← ["A12", "B4", "C7"]APPEND(rooms, "D1")INSERT(rooms, 1, "Lab")REMOVE(rooms, 3)DISPLAY(rooms[2])DISPLAY(LENGTH(rooms))
  1. Write rooms in full after each of lines 2–4.
  2. State what lines 5 and 6 display.
  3. Write the Python statement that displays the same element as line 5.
  4. 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.

Starter
caps ← [6, 0, 3, 3, 10]minimum ← 3result ← []FOR EACH c IN caps{  IF (c ≥ minimum)  {    APPEND(result, c)  }}
  1. Trace the starter and give result.
  2. Give result for minimum 11 and for caps ← [].
  3. Count how many clubs have at least 3 places, and explain why duplicates count separately.
  4. Describe what a variable-based version (cap1 to cap5) would need if 200 clubs were added, compared with this list version.

Exit check

  1. For scores ← [7, 2, 9], what is scores[2] in AP pseudocode and in Python?
  2. 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

  1. Question 1 of 6What does this AP pseudocode display?

    Select one answer.

    AP CSP pseudocode
    clubs ← ["Art", "Chess", "Code"]DISPLAY(clubs[1])
  2. Question 2 of 6What does this program display?

    Select one answer.

    AP CSP pseudocode
    caps ← [5, 8, 2]INSERT(caps, 2, 7)DISPLAY(caps[3])
  3. Question 3 of 6What does this program display?

    Select one answer.

    AP CSP pseudocode
    caps ← [4, 0, 6, 1]REMOVE(caps, 2)DISPLAY(LENGTH(caps))DISPLAY(caps[2])
  4. Question 4 of 6What does this filter display?

    Select one answer.

    AP CSP pseudocode
    caps ← [0, 5, 2, 8]result ← []FOR EACH c IN caps{  IF (c ≥ 3)  {    APPEND(result, c)  }}DISPLAY(result)
  5. Question 5 of 6What does this program display?

    Select one answer.

    AP CSP pseudocode
    names ← ["Art", "Code", "Art", "Art", "Chess"]count ← 0FOR EACH n IN names{  IF (n = "Art")  {    count ← count + 1  }}DISPLAY(count)
  6. Question 6 of 6This procedure keeps every capacity greater than minimum. For which two inputs does it return an empty list? Select two.

    Select exactly two answers (0 of 2 selected).

    AP CSP pseudocode
    PROCEDURE above(caps, minimum){  result ← []  FOR EACH c IN caps  {    IF (c > minimum)    {      APPEND(result, c)    }  }  RETURN(result)}

Your practice so far

Check an answer to see its rationale.

Apply it somewhere new

  1. Question 1 of 4A bus app stores the arrival times of every bus at a stop. Why is a list a better choice than a separate variable for each bus?

    Select one answer.

  2. Question 2 of 4Version 1 computes total ← caps[1] + caps[2] + caps[3]. Version 2 adds every element with FOR EACH. A fourth club is added to caps. Which statement is true?

    Select one answer.

  3. Question 3 of 4A program tracks quiz scores with variables s1 to s25 for a class of 25. The school combines classes into one group of 120. What is the main effect?

    Select one answer.

  4. Question 4 of 4A club finder must show clubs with places left. Its only list is clubNames ← ["Art", "Chess", "Code"]. Why does this collection not serve the purpose?

    Select one answer.

Your practice so far

Check an answer to see its rationale.