01 / 20

Understanding
Lists

Learn how lists store ordered collections of data, enabling powerful operations like searching, inserting, and iterating through elements.

AP Computer Science Principles

What Is a List?

A list is an ordered sequence of elements stored under a single variable name. Each element occupies a specific position, called its index. Lists let you group related data together instead of creating dozens of separate variables.

1 2 3 4 5 "Red" "Blue" "Green" "Gold" "Pink" colors ← a list with 5 elements

Why Lists Matter

Organization
Group related values: a class roster, quiz scores, or pixel colors under one name.
Efficiency
Process thousands of items with a single loop instead of writing repetitive code.
Flexibility
Add, remove, or rearrange elements at any time: lists grow and shrink dynamically.

Without lists, storing 100 student names would require 100 separate variables. Lists make data management scalable and are foundational to every real-world program.

Anatomy of a List

scores ← [ 85 , 92 , 78 , 96 ] index 1 index 2 index 3 index 4

\( \text{LENGTH(scores)} = 4 \)

Core Operations

1
Access: \( \text{list}[i] \) returns the element at index i.
2
Assign: \( \text{list}[i] \leftarrow \text{value} \) replaces the element at index i.
3
Append: \( \text{APPEND}(\text{list}, \text{value}) \) adds a new element to the end.
4
Insert: \( \text{INSERT}(\text{list}, i, \text{value}) \) places a value at index i, shifting everything right.
5
Remove: \( \text{REMOVE}(\text{list}, i) \) deletes the element at index i, shifting everything left.
6
Length: \( \text{LENGTH}(\text{list}) \) returns the number of elements.

Traversing a List

A traversal visits every element exactly once. You can iterate by index or by element value. Both patterns appear on the AP exam.

By Index
FOR i ← 1 TO LENGTH(list)
Use \( \text{list}[i] \) to access each element. Best when you need the position.
By Element
FOR EACH item IN list
item holds the current value. Simpler when position is not needed.

List vs. Variable

List Single Variable
Stores Multiple values One value
Access By index: \( \text{list}[i] \) By name: \( x \)
Size Dynamic: grows/shrinks Fixed: always 1 value
Loop-friendly ✓ Process all at once ✕ Manual per variable
Best for Collections of related data Single, independent values

Working with Lists: Step by Step

1
Create the list and populate it with initial values (or start empty).
2
Choose the right operation: access, modify, append, insert, or remove.
3
Use a loop when you need to process every element (traversal).
4
Track index changes after INSERT or REMOVE: indices shift.
5
Check boundary conditions: never access an index \( < 1 \) or \( > \text{LENGTH(list)} \).

Example 1: Building a List

nums ← [] APPEND(nums, 10) APPEND(nums, 20) APPEND(nums, 30) DISPLAY(nums)
After each APPEND: Step 1: 10 Step 2: 10 20 Step 3: 10 20 30 → [10, 20, 30]

Example 2: Insert and Remove

pets ← ["cat", "dog", "fish"] INSERT(pets, 2, "bird") {pets is now ["cat", "bird", "dog", "fish"]} REMOVE(pets, 4) {pets is now ["cat", "bird", "dog"]}
Before: "cat" "dog" "fish" After INSERT: "cat" "bird" "dog" "fish" shifted → After REMOVE: "cat" "bird" "dog" "fish"

Example 3: Summing a List

grades ← [88, 95, 72, 90] total ← 0 FOR EACH g IN grades { total ← total + g } average ← total / LENGTH(grades) DISPLAY(average)
Iteration trace: g total 88 0 + 88 = 88 95 88 + 95 = 183 72 183 + 72 = 255 90 255 + 90 = 345

\( \text{total} = 345 \)

\( \text{average} = \frac{345}{4} \)

\( = 86.25 \)

Searching a List

names ← ["Ada", "Ben", "Cho", "Dee"] target ← "Cho" found ← false FOR EACH name IN names { IF (name = target) { found ← true } } DISPLAY(found)

Filtering into a New List

scores ← [45, 82, 67, 91, 55] passing ← [] FOR EACH s IN scores { IF (s >= 70) { APPEND(passing, s) } } {passing is now [82, 91]}
scores: 45 82 67 91 55 passing: 82 91

\( \text{Only scores } \ge 70 \text{ kept} \)

Common Mistakes

✕ Off-by-One Index
Accessing \( \text{list}[0] \) in AP pseudocode. Indices start at 1, not 0.
✕ Out-of-Bounds Access
Using an index greater than \( \text{LENGTH(list)} \). Always check bounds before accessing.
✕ Modifying During FOR EACH
Removing elements while iterating with FOR EACH can skip items or cause errors.

List Operations Summary

Operation Pseudocode Effect
Access \( \text{list}[i] \) Returns element at index i
Assign \( \text{list}[i] \leftarrow \text{val} \) Replaces element at index i
Append \( \text{APPEND}(\text{list}, \text{val}) \) Adds val to end
Insert \( \text{INSERT}(\text{list}, i, \text{val}) \) Inserts val at i, shifts right
Remove \( \text{REMOVE}(\text{list}, i) \) Deletes element at i, shifts left
Length \( \text{LENGTH}(\text{list}) \) Returns number of elements
Traverse FOR EACH item IN list Visits every element once

Question 1 of 5

Consider the list \( \text{data} \leftarrow [5, 12, 8, 3] \). What is the value of \( \text{data}[3] \)?

Question 2 of 5

What does APPEND(myList, "x") do?

Question 3 of 5

What is the final value of items?

Question 4 of 5

What value does max hold after this code runs?

Question 5 of 5

What does result contain after this code runs?