List Operations (College Board AP® Computer Science Principles): Revision Note
List operations & syntax
What operations can be performed on a list?
A list stores an ordered collection of elements that can be accessed, modified and managed using specific operations
Lists can also be referred to as arrays
The AP exam uses 1-based indexing, so the first element in a list is at index 1
Accessing an index less than 1 or greater than the length of the list produces an error
Operation | Exam notation | Description |
|---|---|---|
Empty list |
| Create a list with no elements |
Access by index |
| Retrieves the element at position |
Assign to element |
| Replaces the element at position |
Assign list to variable |
| Creates a list with the specified values |
INSERT |
| Inserts |
APPEND |
| Adds |
REMOVE |
| Removes the element at position |
LENGTH |
| Returns the number of elements currently in the list |
Copy list |
| Copy the content of one list to another |
How INSERT, APPEND and REMOVE change a list
aList ← ["a", "b", "c"]
INSERT(aList, 2, "x")produces["a", "x", "b", "c"](inserts at position 2, pushes "b" and "c" right)APPEND(aList, "d")produces["a", "x", "b", "c", "d"](adds to the end)REMOVE(aList, 1)produces["x", "b", "c", "d"](removes "a", everything shifts left)
List iteration methods
How are lists traversed in a program?
A traversal is the process of accessing each element in a list one at a time
A complete traversal visits every element in the list from start to finish
A partial traversal stops early once a condition is met, without visiting every element
FOR EACH loops
The
FOR EACHloop is designed specifically for traversing listsIt automatically accesses each element in order without needing to manage an index variable
scores ← [85, 92, 78, 95]
total ← 0
FOR EACH score IN scores
{
total ← total + score
}
average ← total / LENGTH(scores)
DISPLAY(average)
Output:
87.5
Common list algorithms
These are standard patterns that appear frequently on the AP exam
Algorithm | Purpose | How it works |
|---|---|---|
Find minimum/maximum | Identify the smallest or largest value in a list | Set a variable to the first element, then compare each remaining element and update if smaller/larger |
Compute sum/average | Calculate the total or mean of all values | Use a running total variable, add each element, divide by |
Linear search | Determine whether a value exists in a list | Check each element one at a time until the value is found or the end of the list is reached |
Examiner Tips and Tricks
INSERT and REMOVE are the most commonly tested list operations because they change the positions of other elements. When tracing code that uses INSERT, remember that elements at and after the insertion point shift right. When REMOVE is used, elements after the removed position shift left. Always track the list length as it changes.
For the AP Create Performance Task, your program must include a list that manages complexity — on exam day be prepared to explain how your list stores data and how accessing or modifying its elements contributes to your program's purpose
Worked Example
A program contains the following code segment.
animals ← ["cat", "dog", "bird"]
INSERT(animals, 2, "fish")
REMOVE(animals, 4)
DISPLAY(animals)
What is displayed as a result of executing the code segment?
(A) ["cat", "fish", "dog"]
(B) ["cat", "fish", "dog", "bird"]
(C) ["cat", "fish", "bird"]
(D) ["cat", "dog", "fish"]
[1]
Answer:
(A) ["cat", "fish", "dog"] [1 mark]
INSERT at position 2 shifts "dog" and "bird" right to produce
["cat", "fish", "dog", "bird"]; REMOVE at position 4 then removes "bird", leaving["cat", "fish", "dog"]
Unlock more, it's free!
Was this revision note helpful?