Topic 3.10: Lists | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.10 Lists
3.10
Big Idea 3 • Algorithms & Programming

Lists

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 List Navigator game 💻 Live Python editor AAP-2.N • AAP-2.O

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
📈 Exam weight: Lists power a large share of Big Idea 3 tracing and spot-the-bug items, and a list is a required element of the Create Task. The 0-indexed versus 1-indexed gap and out-of-range indexing are among the most common traps on the whole exam.
💡 Think about this first

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.

🎯 What the exam expects

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.

✍ Mini Exercise 1 • Predict the output
This is Python, so it is 0-indexed. What does it print?
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.

⚠ The shift trap

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.

✍ Mini Exercise 2 • Trace the shift
After the insert, values shift right. What does 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.

✍ Mini Exercise 3 • Fill in the blank
The loop adds every element. Type the exact number it prints.
vals = [3, 6, 1]
n = 0
for v in vals:
    n = n + v
print(n)
prints:

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:
📋 Create Performance Task • A list plus a traversal manages complexity

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:

scores <- [8, 5, 9, 7] total <- 0 FOR EACH s IN scores { total <- total + s } average <- total / LENGTH(scores) DISPLAY(average)

In your written response, name the list and describe how the traversal uses it:

  • "The list scores stores 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 scores to build the running total, so the algorithm works for a list of any length."
  • "Dividing total by LENGTH(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 →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Indexing
Predict first: in Python, which element sits at index 1?
This list is created in Python, which is 0-indexed. What does the code display?
t = [5, 10, 15, 20] print(t[1])
Incorrect. 5 is at index 0 because Python counts from 0. Index 1 is the second element.
Correct. In Python index 0 is 5 and index 1 is 10, so it prints 10. Reading index 1 as the first element is the 1-indexed trap.
Incorrect. 15 is at index 2. You may be counting from 1, but Python counts from 0.
Incorrect. 20 is the last element at index 3. Index 1 is the second element, 10.
Question 2 of 6NOT question
In AP pseudocode a list p contains 4 elements and is indexed starting at 1. Which expression does NOT refer to a valid element of p?
Incorrect. In 1-indexed pseudocode, p[1] is the first element, which is valid.
Incorrect. p[3] is the third element, which is valid in a 4 element list.
Incorrect. p[4] is the last element, because the last index equals the length in AP pseudocode.
Correct. Valid indices run from 1 to 4 (the length). p[5] is past the end, so it is out of range.
Question 3 of 6Spot the bug
This is meant to print every element of s, one per line, but it crashes. What is the bug?
s = [7, 4, 9] k = 0 while k <= len(s): print(s[k]) k = k + 1
Incorrect. Starting at 0 is right in Python. The problem is the loop runs one step too far at the end.
Correct. len(s) is 3, but valid indices are 0, 1, 2. When k reaches 3 the test k <= 3 is still true, so s[3] runs past the end and crashes. Use k < len(s).
Incorrect. Printing s[k] each pass is the intended behavior; the bug is the loop bound.
Incorrect. len(s) returns 3, not 4, and that is exactly why k <= len(s) reaches the invalid index 3.
Question 4 of 6Spot the bug
This should print the last element of x after the first element is removed, but it crashes. Why?
x = [10, 20, 30, 40] x.pop(0) print(x[3])
Incorrect. pop(0) does remove the first element, 10. The list definitely changes.
Correct. After pop(0) the list is [20, 30, 40] with valid indices 0 to 2. The old last index 3 no longer exists, so x[3] is out of range.
Incorrect. Indexing right after pop is legal. The crash is because the list got shorter.
Incorrect. x[3] means the element at index 3, not the value 3. That index is now out of range.
Question 5 of 6I, II, III
A Python list 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)]
Incorrect. I gives 8, but II also gives 8 because len(v) - 1 is 3.
Correct. v[3] is 8, and v[len(v) - 1] is v[3] which is 8. III uses v[len(v)] = v[4], which is out of range, not 8.
Incorrect. III is v[4], which is past the end and raises an error, so III does not evaluate to 8.
Incorrect. III is v[len(v)] = v[4], the classic off-by-one past the last index. It is out of range, not 8.
Question 6 of 6Trace
Predict first: how many times does the counter increase?
What does this counting traversal display?
n = [3, 8, 3, 5, 3] c = 0 for k in n: if k == 3: c = c + 1 print(c)
Incorrect. Count the 3s again: they appear at the first, third, and fifth positions.
Correct. The loop visits every element and adds 1 each time the value equals 3. There are three 3s, so it prints 3.
Incorrect. 5 is the length of the list, not the count of matches. Only elements equal to 3 increase c.
Incorrect. c starts at 0 but increases each time a 3 is found, and there are three of them.
🎮 Lesson Game
List Navigator
Trace each list operation and predict what it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what does the list show?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • access by index
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
Target output:
10
50
Level 2 • Update
Problem 2 of 8 • change one element
Change the middle element of data (index 1) to 99, then print the whole list. Target output: [3, 99, 9]
Level 3 • Append + length
Problem 3 of 8 • grow the list
Append 12 to the end of s, then print how many elements the list now has. Target output: 3
Level 4 • Fix the bug
Problem 4 of 8 • loop bounds
This should print every element of 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
Level 5 • Traversal sum
Problem 5 of 8 • add them all up
Use a loop to add up every number in vals and print the total. Target output: 50
Level 6 • Create Task style
Problem 6 of 8 • count matches
Count how many values in scores are passing (at least 70) and print the count. Target output: 3
Level 7 • Challenge
Problem 7 of 8 • you write the whole program
Open ended. Write the entire program yourself. Given the fixed list data, print two lines:
  • first line: the sum of all elements
  • second line: the largest element
Use loops or built-ins, but the list is fixed (no input). Target output:
141
45
Level 8 • Challenge
Problem 8 of 8 • count, then list them
Open ended. Write the entire program yourself. Given the fixed list temps, do this in order:
  • print how many readings are strictly above 90
  • then print each reading above 90, in order, one per line
Target output:
3
92
100
95

Frequently Asked Questions

They just use different starting points. Python is 0-indexed, so the first element is at index 0 and the last is at index 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.
In Python it is 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.
They shift. Inserting pushes every later element to a higher index to make room. Removing pulls every later element to a lower index to close the gap, and the list gets one shorter. Never assume an element kept its old index across an insert or remove.
Traversing means visiting every element in order, usually with a loop, so you can sum values, search for a target, or count matches. In Python, for element in list gives you each value directly, while for i in range(len(list)) gives you each index.
A list lets your program manage many related values under one name instead of dozens of separate variables, which earns the collection point. Traversing that list with a loop earns toward the algorithm point. Describe the list by name and explain how the traversal uses it in your written response.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]