Topic 3.10: Lists | AP CSP Big Idea 3 | APCSExamPrep.com
Lists
After this lesson, you will be able to:
- Create a list and access or update an element by its index
- Explain the 0-indexed Python versus 1-indexed pseudocode difference and find the last valid index
- Append, insert, and remove elements, and predict how the others shift
- Find the length of a list and use it to bound a traversal safely
- Traverse a list with a loop to sum, search, or count elements
- Use a list and a traversal to manage complexity in your Create Performance Task
A playlist does not use a separate variable for every song. It uses one list and reaches song number 4 by its position. But here is the catch that costs points every year: Python calls the first song position 0, while the AP pseudocode reference sheet calls it position 1. Same songs, different numbers. This lesson makes that gap, and every list operation around it, automatic.
What a List Is, and the Indexing Gap
A list stores many values under one name. Instead of a, b, c, and d, you keep four related values in one ordered collection and reach any of them by its position, called an index. Lists are how a program manages a group of data whose size you may not know in advance.
Here is the single most tested idea in this topic, and it trips up students on every exam: the two notations you will see count from different starting points. Python lists are 0-indexed, so the first element is at index 0 and the last is at index len(list) - 1. AP pseudocode lists are 1-indexed, so the first element is at index 1 and the last is at index LENGTH(list). Same list, same values, different index numbers.
nums = [10, 20, 30, 40] print(nums[0]) print(nums[3])
nums <- [10, 20, 30, 40] DISPLAY(nums[1]) DISPLAY(nums[4])
Look closely: both panels print 10 and then 40, the first and last values. But Python reaches the first value with nums[0] and the last with nums[3], while AP pseudocode reaches the exact same values with nums[1] and nums[4]. Read every index question by first asking which notation you are in.
On the reference sheet, aList[1] is the FIRST element and aList[LENGTH(aList)] is the LAST. When a question is written in Python instead, index 0 is the first element and len(aList) - 1 is the last. Decide the notation before you count, and the off-by-one traps disappear.
data = [4, 8, 15, 16] print(data[2])
Updating, Appending, and Length
Once a list exists you can change an element in place by assigning to its index: nums[1] = 99 replaces whatever was at index 1. You can grow the list from the end with append, which always adds one new element after the current last one. And len(nums) reports how many elements the list currently holds, a value that changes every time you add or remove.
nums = [10, 20, 30] nums[1] = 99 nums.append(40) print(nums) print(len(nums))
nums <- [10, 20, 30] nums[2] <- 99 APPEND(nums, 40) DISPLAY(LENGTH(nums))
Trace it: the list starts as three values, nums[1] = 99 overwrites the middle one, and append(40) tacks a fourth on the end, giving [10, 99, 30, 40]. Because there are now four elements, len(nums) is 4. In AP pseudocode the same edit uses nums[2] <- 99 (index 2 is the middle element when counting from 1) and APPEND, and LENGTH(nums) returns 4.
Insert and Remove: Everything Shifts
Inserting or removing an element in the middle of a list does more than change one spot. Every element after the change shifts to a new index to keep the list packed with no gaps. This shifting is where index bugs are born, because a position you were pointing at a moment ago may now hold a different value.
letters = ["a", "b", "c"] letters.insert(1, "x") letters.pop(0) print(letters)
letters <- ["a", "b", "c"] INSERT(letters, 2, "x") REMOVE(letters, 1) DISPLAY(letters)
Start with ["a", "b", "c"]. In Python, insert(1, "x") drops "x" at index 1 and pushes "b" and "c" right, giving ["a", "x", "b", "c"]. Then pop(0) removes the element at index 0, and everything shifts left, leaving ["x", "b", "c"]. The pseudocode reaches the identical result, but its INSERT uses index 2 and its REMOVE uses index 1 because it counts from 1.
After you remove an element, the list gets one shorter and the largest valid index drops by one. Code that still reaches for the old last index will run past the end and crash. After you insert, values move to higher indices. Never assume an element kept its old position across an insert or remove.
t[2] hold?t = [1, 2, 3] t.insert(1, 9) print(t[2])
Traversing a List with a Loop
A traversal visits every element of a list one at a time, usually with a loop. Traversal is how you sum values, search for a target, or count how many match a rule, no matter how long the list is. The most common Python form is for element in list, which hands you each value in order without you managing an index at all.
You can also traverse by index with for i in range(len(list)). That form is powerful, but it is exactly where the bounds trap lives: the valid indices are 0 through len(list) - 1, so you loop range(len(list)). If you loop one step too far, you index past the end and the program crashes.
scores = [8, 5, 9, 7] total = 0 for s in scores: total = total + s print(total)
scores <- [8, 5, 9, 7] total <- 0 FOR EACH s IN scores { total <- total + s } DISPLAY(total)
This traversal starts a running total at 0, then adds each score as the loop visits it: 0, then 8, then 13, then 22, then 29. After the last element it prints 29. The pseudocode FOR EACH loop does the same thing, visiting every element in order.
vals = [3, 6, 1] n = 0 for v in vals: n = n + v print(n)
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| List | An ordered collection of values stored under one name | [10, 20, 30] |
| Index | The position number used to reach one element | nums[0] |
| 0-indexed (Python) | First element is index 0; last is len(list) - 1
|
nums[3] is 4th |
| 1-indexed (pseudocode) | First element is index 1; last is LENGTH(list)
|
nums[1] is 1st |
| Append | Add one element to the end of the list | nums.append(40) |
| Insert / Remove | Add or delete inside the list; later elements shift | nums.insert(1, 9) |
| Traversal | Visiting every element, usually with a loop | for s in scores: |
The Create Task rubric awards a point for using a list (a collection type) to manage complexity, and another for an algorithm that includes iteration. A single list traversal earns toward both at once, which is why lists are the backbone of a strong Create Task program.
A point-earning example
Suppose your program stores quiz scores in a list and computes the average:
In your written response, name the list and describe how the traversal uses it:
- "The list
scoresstores multiple related values under one name, so the program manages many scores without a separate variable for each one." - "The loop traverses every element of
scoresto build the runningtotal, so the algorithm works for a list of any length." - "Dividing
totalbyLENGTH(scores)produces the average, a result the program could not compute without both the list and the traversal."
The trap to avoid
When you describe an index in your response, match the notation you actually used. If your code is Python, the last element is scores[len(scores) - 1], not scores[len(scores)]. An off-by-one in your own writeup signals you do not fully understand your list access. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
p contains 4 elements and is indexed starting at 1. Which expression does NOT refer to a valid element of p?s, one per line, but it crashes. What is the bug?x after the first element is removed, but it crashes. Why?v holds [2, 4, 6, 8] and is 0-indexed. Which of the following expressions evaluate to 8?- I. v[3]
- II. v[len(v) - 1]
- III. v[len(v)]
nums is a Python list, so it is 0-indexed. Print two lines:- first line: the element at index
0 - second line: the element at index
4
10
50
data (index 1) to 99, then print the whole list. Target output: [3, 99, 9]
12 to the end of s, then print how many elements the list now has. Target output: 3
t, one per line, but it crashes with an index error because the loop runs one step too far. Fix the bound. Target output:5
10
15
vals and print the total. Target output: 50
scores are passing (at least 70) and print the count. Target output: 3
data, print two lines:- first line: the sum of all elements
- second line: the largest element
141
45
temps, do this in order:- print how many readings are strictly above
90 - then print each reading above
90, in order, one per line
3
92
100
95
Frequently Asked Questions
len(list) - 1. AP pseudocode is 1-indexed, so the first element is at index 1 and the last is at index LENGTH(list). Decide which notation a question uses before you count positions.len(list) - 1, because indexing starts at 0. In AP pseudocode it is LENGTH(list), because indexing starts at 1. Reaching for one position past the last valid index is the most common list error and it causes an out-of-range crash.for element in list gives you each value directly, while for i in range(len(list)) gives you each index.🔗 Continue studying
The Superpack includes an editable Topic 3.10 slide deck with animated index-shift traces, a 0-indexed versus 1-indexed drill bank, a print-ready list-traversal worksheet, and a unit quiz. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]