Skip to main content
AP CSP course

C36 · Topic 3.10 · 40 minutes · AAP-2.N, AAP-2.O

Traversing and filtering lists

By the end you can:

  • Visit every required element.
  • Retain duplicates and input order in a filter.
  • Place return after a complete traversal.

Before this lesson: topics 3.2, 3.8.

Index
A position used to access an element; AP CSP list positions start at 1.
Traversal
Visiting list elements systematically to compute a result.

Understand

A traversal applies a rule to each relevant element. A filter builds a new list containing the entries satisfying a condition; a counter instead accumulates how many qualify. Initialize the result once before the traversal and return only after all required elements have been considered.

Repeated values still represent distinct occurrences. A filter that appends on each qualifying visit preserves duplicates and relative order. Test a list with multiple matches, a value equal to the threshold and an empty list, since a first-match or strict-boundary error may pass an ordinary-looking single-match example.

Worked example

Filter[2,7,3,7,0]for values at least 5.

result ← []
FOR EACH value IN values
{ IF(value ≥ minimum) { APPEND(result,value) } }
RETURN(result)
  1. 2 fails; the first 7 appends, producing[7].
  2. 3 fails; the second 7 appends, producing[7,7].
  3. 0 fails; return[7,7]after the traversal.

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.

  • AP one-based indexing
  • Python index comparison
  • Index shifting
  • Invalid access terminates
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: Read, replace, append, insert and remove
  1. List (0–100; max 8)10,20,30Waiting
  2. OperationinsertWaiting
  3. AP position (0–9)2Waiting
  4. Value (0–100)99Waiting

Ready to explore: Read, replace, append, insert and remove

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.

Check a misconception

Claim: Returning after the first match is a complete filter.

It ends the procedure before later qualifying entries can be collected.

Guided practice

Trace filtering[5,5]with minimum 5 and explain both inclusions.

Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.

Independent transfer

What should this procedure return for[]and for[1,2]with minimum 5?

Exit check

How would you count matches using the filtered result?

Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.

Check yourself

  1. Question 1 of 2What is the inclusive filter result for[3,8,3]with minimum 3?

    Select one answer.

  2. Question 2 of 2Which placement supports a full filter?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring