Lists (College Board AP® Computer Science Principles): Revision Note
List fundamentals
What is a list?
A list is a data structure that allows multiple values to be stored in a single variable
Lists can also be referred to as an array
A list is an ordered sequence, meaning its elements are arranged in a specific order that is maintained
Each element holds a current value that can be read or updated
Assignment to a list element works the same way as with single variables: a value can be assigned to a specific position
scores ← [85, 92, 78, 90]
scores[2] ← 80
In this example, the element at index 2 is updated from 92 to 80 (the current value at that position is replaced)
Indices and index references
Each element has a position identified by an index (also called an index reference)
In AP CSP pseudocode, list indices start at 1 (the first element is at index 1, the second at index 2, and so on)
Indices allow programs to access, update, or remove specific elements without affecting the rest of the list
Each index position is unique: no two elements share the same index
names ← ["Alex", "Jordan", "Sam"]
DISPLAY(names[1])
This displays
"Alex"because index 1 refers to the first element in the list
List properties and abstraction
What properties do lists have?
Lists can contain mixed element types: a single list can hold numbers, strings, and Booleans together
Grouped items in a list share a common purpose or context (for example, a list of test scores or a list of student names)
List initialization is the process of creating a list and assigning its starting values
A list can also be initialized as empty and have elements added during program execution
Property | Description | Example |
|---|---|---|
Mixed types | A list can hold values of different data types |
|
Grouped items | Elements are related by a shared purpose | A list of all scores from one test |
Initialization | A list is created with or without starting values |
|
Lists as abstraction
A list is a form of list abstraction because it allows a programmer to work with a named collection of related values as a single unit
Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation
Programmers work with a list through its name and indices, without needing to know how it is stored in memory
Using lists simplifies development by reducing the number of variables needed and keeping related data together
Lists also improve maintenance: if the data structure needs to change, updates are made in one place rather than across many separate variables
Index management
How are indices managed in a list?
The index range of a list runs from 1 to the length of the list in AP CSP pseudocode
Accessing an index outside this range (below 1 or above the list length) causes an error because the position is out of bounds
Programs must account for bounds when adding, removing, or accessing elements to avoid runtime errors
When elements are inserted or removed, the indices of subsequent elements shift accordingly
colors ← ["red", "green", "blue"]
DISPLAY(colors[4])
This causes an error because the list only contains 3 elements, so index 4 is out of bounds
Examiner Tips and Tricks
AP CSP pseudocode uses 1-based indexing — the first element is at index 1, not index 0. This is a common source of errors on the exam, especially if you are used to programming languages that start at 0. When tracing code that modifies a list, track which elements shift position after an insertion or removal.
For the AP Create Performance Task, you must include a list in your program — demonstrate that you can store, access, and process list elements meaningfully.
Worked Example
Consider the following code segment.
animals ← ["cat", "dog", "fish"]
animals[2] ← "bird"
DISPLAY(animals)
What is displayed when this code segment is executed?
(A) ["cat", "dog", "fish"]
(B) ["cat", "bird", "fish"]
(C) ["bird", "dog", "fish"]
(D) ["cat", "dog", "bird"]
[1]
Answer:
(B) ["cat", "bird", "fish"] [1 mark]
animals[2] ← "bird"replaces "dog" at index 2 with "bird"; the elements at indices 1 and 3 are unaffected
Unlock more, it's free!
Was this revision note helpful?