Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.10 Lists

⌨️AP Computer Science Principles
Unit 3 Review

3.10 Lists

Written by the Fiveable Content Team • Last updated September 2025
Verified for the 2026 exam
Verified for the 2026 examWritten by the Fiveable Content Team • Last updated September 2025
⌨️AP Computer Science Principles
Unit & Topic Study Guides
Pep mascot

Basic List Operators

On the AP exam, you'll be asked to evaluate some basic operations on lists.

** Remember, the AP Pseudocode's index starts at 1.**

Pep mascot
more resources to help you study

Accessing an element by index

This operation allows you to single out an element in a list based on its index number. You can then interact with only this element.

grocery_list = ["milk", "eggs", "cheese"]
print (grocery_list[0])

The code's output: 

milk

Assigning the value of an element of a list to a variable

This allows you to assign a variable to a certain element within a list, changing the element. Note that you wouldn't use this operation to add new values to the list, only to change ones already existing.

grocery_list = ["milk", "eggs", "cheese"]
change = "soap"
grocery_list[2] = change
print (grocery_list)

The code's output: ["milk", "eggs", "soap"]

Assigning a value to an element outright

Here's an example in Python:

grocery_list = ["milk", "eggs", "cheese"]
grocery_list[2] = "fish"
print (grocery_list)

The code's output: ["milk", "eggs", "fish"] 

Assigning the value of one element in the list to another

Like this!

grocery_list = ["milk", "eggs", "cheese"]
grocery_list[0] = grocery_list[2]
print (grocery_list)

The code's output: ["cheese", "eggs", "cheese"]

Adding and Removing Elements

Inserting elements at a given index

This allows you to insert a value into the index position you want. It will increase the length of the list and shift everything greater than or equal to that index down by one place. For example, if you were to insert a new value at the index value 4, what was originally there will move to the index value 5, 5 will move to 6, and so on.

grocery_list = ["milk", "eggs", "cheese"]
grocery_list.insert (2, "butter")
print (grocery_list)

The code's output: ["milk", "eggs", "butter", "cheese"]

Adding elements to the end of the list

This allows you to add values to the end of your list.

grocery_list = ["milk", "eggs", "cheese"]
grocery_list.append ("flour")
print (grocery_list)

The code's output: ["milk", "eggs", "butter", "flour"]

Removing elements

You can also remove elements. 

In Python, you remove items based on element value rather than index number.

grocery_list = ["milk", "eggs", "cheese"]
grocery_list.remove ("eggs")
print (grocery_list)

The code's output: ["milk", "cheese"]

Determining the length of a list

This will tell you what the length of your list is.

grocery_list = ["milk", "eggs", "cheese"]
print (len (grocery_list))

The code's output: 3

Looping through Lists

You can also use loops to traverse, or go through, a list. This can either be a complete traversal or a partial traversal, depending on what your loop specifies.

Common algorithms used with lists will often find the maximum or minimum value inside the list or the average. 

Linear search or sequential search algorithms check each element of a list, in order, until the desired value is found or all elements in the list have been checked.

Complete Traversal

In Python, you can perform a complete traversal of a list using either a for loop or a while loop. Both implementations are shown below. 

grocery_list = ["milk", "eggs", "cheese"]

# for loop implementation
for element in grocery_list:
  print(element)

The code's output: 

milk

eggs

cheese

grocery_list = ["milk", "eggs", "cheese"]

# while loop implementation
i = 0
while i < len(grocery_list):
  print(grocery_list[i])
  i += 1

The code's output:

milk

eggs

cheese

Partial Traversal

In Python, you can also perform a partial traversal of a list using either a for loop or a while loop. Both implementations are shown below. 

grocery_list = ["milk", "eggs", "cheese", "apples"]

# for loop implementation
start_index = 1
end_index = 3

for i in range(start_index, end_index + 1):
  print(grocery_list[i])

The code's output:

eggs

cheese

apples

grocery_list = ["milk", "eggs", "cheese", "apples"]

# while loop implementation
i = 1
while i < 4:
  print(grocery_list[i])
  i += 1

The code's output:

eggs

cheese

apples

Frequently Asked Questions

How do I write a `for` loop to go through every element in a list?

Use the exam’s list traversal constructs. Two common, AP-aligned ways: 1) FOR EACH style (preferred on the reference sheet): FOR EACH item IN aList { // use item directly } This assigns item to each element from the first to last (complete traversal). It’s simple for sums, counts, or checks. 2) Index-based FOR using one-based indexing and LENGTH: FOR i ← 1 TO LENGTH(aList) { x ← aList[i] // use x or modify aList[i] } Use this when you need the index (e.g., REMOVE or INSERT at a specific position). Remember: use one-based indices on the AP exam (aList[1] is first). Traversals can be complete or partial (stop early for a linear search). The AP reference sheet shows FOR EACH and LENGTH; practice these patterns (Fiveable topic study guide: https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h). For more drills, try the practice set (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the difference between `INSERT` and `APPEND` when adding things to a list?

INSERT lets you put a value at a specific index and shifts everything at that index and to the right one place; APPEND always adds the value to the end of the list. Both increase the list’s length by 1. Remember AP uses one-based indexing (first element is aList[1]). Example: aList = [A, B, C] - INSERT(aList, 2, X) → [A, X, B, C] (X goes to index 2; B and C shift right) - APPEND(aList, X) → [A, B, C, X] (X goes to the end) These are exactly the list procedures on the Exam Reference Sheet and in the CED (EK AAP-2.N.1). If you want more practice with list operations and indexing, check the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and the unit practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

Why does my code say "index out of bounds" when I try to access `aList[0]`?

You're getting "index out of bounds" because on the AP exam/reference sheet lists use one-based indexing: the first element is aList[1], not aList[0] (EK AAP-2.N.1). Valid indices are 1 through LENGTH(aList). So aList[0] is outside that range and causes the error. Fixes: - If your algorithm follows the CED/exam reference, start at index 1 (e.g., position ← 1; WHILE position ≤ LENGTH(aList) { ... }). - If you're coding in a language that uses zero-based indexing (like Python or JavaScript), use aList[0] only when that language’s lists are zero-based—be consistent with the language’s rules. - Always check LENGTH(aList) before accessing an index or use safe traversals like FOR EACH item IN aList (EK AAP-2.O.3) to avoid manual index mistakes. For a quick review of list indexing and examples, see the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h). For extra practice problems, try the AP practice bank (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the syntax for accessing the first element of a list in AP pseudocode?

In AP pseudocode (one-based indexing), you access the first element of a list with aList[1]. For example: - Read the first element into a variable: x ← aList[1] - Assign a new value to the first element: aList[1] ← 42 This matches the Exam Reference Sheet and EK AAP-2.N.1 (first element is at index 1). When you do a FOR EACH item IN aList, item is assigned the first element first, then the second, etc. Practice these forms (indexing, LENGTH, APPEND, INSERT, REMOVE) for the exam; the CED tests writing and evaluating list-index expressions (LO AAP-2.N and AAP-2.O). For a quick review, see the Topic 3.10 study guide on Fiveable (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and more unit resources (https://library.fiveable.me/ap-computer-science-principles/unit-3). For extra practice, check Fiveable’s practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

When should I use `FOR EACH` versus a regular `for` loop to traverse a list?

Use FOR EACH when you only need to read every element in order (a complete traversal) and don’t care about the element’s index. The CED’s exam reference has the exact form: FOR EACH item IN aList { ... }—item gets each value from first to last (EK AAP-2.O.3). It’s concise and less error-prone for sums, averages, min/max, or simple linear searches (EK AAP-2.O.4–O.5). Use a regular for loop (with an index like i from 1 to LENGTH(aList)) when you need the index or you’ll change the list: - you must assign or remove elements at a specific index (aList[i] ← x, INSERT, REMOVE) (EK AAP-2.N.1), - you need a partial traversal that starts/stops at specific positions, - you need the position for output or algorithm logic (e.g., “first occurrence” index). Note: AP excludes parallel traversals using the same index for multiple lists (EK AAP-2.O.1). For more examples and practice, check the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and the AP practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

I'm confused about why list indexing starts at 1 instead of 0 like in Java - can someone explain?

Good question—AP CSP uses one-based indexing because the Exam Reference Sheet and the CED define list operations that way (aList[1] is the first element). That choice keeps the pseudocode simpler and closer to how people naturally count (1st, 2nd, 3rd), and it makes descriptions of INSERT(aList, i, value), REMOVE(aList, i), and LENGTH(aList) easier to state and reason about when writing or tracing algorithms. Java and many programming languages use zero-based indexing for efficiency and array-address math, but for the AP exam you must follow the CED/pseudocode convention (aList[i], first element at i = 1). If you work in Java on practice problems, just remember to convert: AP_index = Java_index + 1 (or Java_index = AP_index - 1). Review Topic 3.10 in the AP study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles) to get comfortable switching conventions.

How do I find the maximum value in a list using a loop?

Use a linear traversal and keep a variable for the current max. On the AP reference sheet you can use FOR EACH or an index-based loop and one-based indexing (aList[1] is first). Example in exam-style pseudocode: IF LENGTH(aList) = 0 THEN RETURN(undefined) // or handle empty list END IF max ← aList[1] FOR EACH item IN aList { IF item > max { max ← item } } RETURN(max) Why this works: you do a complete traversal (EK AAP-2.O.1) so every element is checked (EK AAP-2.O.2). Initializing max to the first element avoids wrong results when all values are negative. You can also use an index loop from 1 to LENGTH(aList) and compare aList[i] to max. This pattern is exactly the “determine maximum” example in the CED (LO AAP-2.O / EK AAP-2.O.4). For a quick refresher see the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

What happens to the other elements when I use `REMOVE(aList, i)` to delete something from the middle?

REMOVE(aList, i) removes the element at index i, decreases LENGTH(aList) by 1, and shifts every element that was at an index > i one position to the left (so the element that was at i+1 becomes i, i+2 becomes i+1, etc.). Remember AP uses one-based indexing (aList[1] is first). That shift can change which items you’ll visit if you’re iterating with an index—if you remove while stepping forward with position, you might skip the element that moved into index i. Common fixes: iterate backwards when removing by index, or only remove after finishing a full traversal. See the AP exam reference sheet list ops and the Topic 3.10 study guide for examples (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h). For extra practice, check the AP practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

Can someone trace through this linear search algorithm step by step?

Linear (sequential) search checks elements in order until it finds the target or reaches the end (partial vs complete traversal—EK AAP-2.O.1, EK AAP-2.O.5). Trace example with one-based indexing (aList[1] is first): Given aList = [7, 3, 9, 3, 5], target = 3, position ← 1 1. position = 1 → aList[1] = 7. Is 7 = 3? No. Increment position → 2. (partial traversal) 2. position = 2 → aList[2] = 3. Is 3 = 3? Yes. Stop—target found at index 2. (algorithm ends early) If the target weren’t present, you’d continue checking positions 3, 4, 5, then stop after position > LENGTH(aList) and report “not found” (complete traversal). This matches the AP reference description and is the kind of list traversal you should be able to write/evaluate on the exam (see Topic 3.10 study guide: https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h). For more practice problems, try the AP practice bank (https://library.fiveable.me/practice/ap-computer-science-principles).

How do I write code to calculate the average of all numbers in a list?

Use the exam reference-sheet list traversal and LENGTH. Average = (sum of all elements) / LENGTH(aList). In AP-style pseudocode: ``` PROCEDURE average(aList) { IF LENGTH(aList) = 0 THEN RETURN 0 // or handle “no elements” error sum ← 0 FOR EACH item IN aList { sum ← sum + item } avg ← sum / LENGTH(aList) RETURN avg } ``` Notes: this uses FOR EACH item IN aList (EK AAP-2.O.3) for a complete traversal and LENGTH(aList) (EK AAP-2.N.1). Watch out for empty lists (divide-by-zero). On the AP exam you can write this exactly in the reference-sheet style. For extra practice on lists and averages, check the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and thousands of practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

What's the difference between complete traversal and partial traversal of a list?

Complete traversal: your loop or FOR EACH visits every element from the first to the last (EK AAP-2.O.1). Use this when you need information about the whole list—e.g., compute a sum/average, find min/max, or count occurrences. A complete traversal accesses aList[1] through aList[LENGTH(aList)] (or uses FOR EACH item IN aList) so every element is checked. Partial traversal: the loop stops before checking every element because you already have what you need. A common case is linear (sequential) search that stops when it finds the target value (EK AAP-2.O.5). Partial traversals are faster when you can finish early, but they won’t guarantee info about unseen elements (so you can’t compute a total or true min without a complete traversal). For AP exam prep, know when an algorithm requires complete vs partial traversal and be able to write/evaluate FOR EACH or index-based loops accordingly. Review Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

Why am I getting the wrong answer when I try to assign `x ← aList[LENGTH(aList)]` to get the last element?

If you’re getting the wrong value, two common reasons: 1) Indexing convention mismatch—The AP CED uses one-based indexing (aList[1] is the first element), so on the exam the last element is aList[LENGTH(aList)]. But most real languages (Python, Java, JavaScript) use zero-based indexing, so the last element there is aList[LENGTH(aList) - 1]. If your environment is zero-based, aList[LENGTH(aList)] will be out of range or return the wrong item. 2) Empty list—If aList is empty, LENGTH(aList) = 0, and aList[0] (or aList[1] in one-based) is invalid. Always check for LENGTH(aList) > 0 before accessing the last element. Reminder: use the AP exam reference (one-based) for CED-style questions and practice both conventions on real coding platforms. For a quick refresher on lists for AP CSP, see the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).

How do I insert a value at a specific position without overwriting what's already there?

Use INSERT, not direct assignment. On the AP reference sheet lists are one-based, so aList[1] is first. If you do aList[i] ← value you overwrite the item at i. Instead use INSERT(aList, i, value). INSERT shifts every element at index ≥ i one position to the right, increases LENGTH(aList) by 1, and puts value at index i—so nothing gets overwritten. If you want to add at the end, APPEND(aList, value) places value at index LENGTH(aList)+1. Quick example (conceptual): aList = [10, 20, 30]; INSERT(aList, 2, 15) → [10, 15, 20, 30]. This behavior (INSERT, APPEND, LENGTH, one-based indexing) is on the Exam Reference Sheet and is in the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h). For more practice problems, check Fiveable’s practice set (https://library.fiveable.me/practice/ap-computer-science-principles).

I don't understand how `FOR EACH item IN aList` works - does it change the original list?

Short answer: No—FOR EACH item IN aList by itself does not change the original list. EK AAP-2.O.3 in the CED says the loop variable item is assigned the value of each element in order and the block runs for each assignment. That means you get a copy of the element to work with. If you only change item (e.g., item ← item + 1), you’re just changing the loop variable—aList stays the same. To change the list you must use list indexing or list procedures from the exam reference sheet (EK AAP-2.N.1). For example: - To modify an element: aList[i] ← aList[i] + 1 inside a loop that tracks i, or - Use INSERT/APPEND/REMOVE to change length/content. If you want examples or practice questions on list traversal and mutations, check the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and the practice bank (https://library.fiveable.me/practice/ap-computer-science-principles).

What are some common algorithms that AP expects me to know for working with lists?

On lists the AP expects you to know a handful of common, testable algorithms and operations: - Traversal: complete/partial traversals using FOR EACH item IN aList or index-based loops (one-based indexing). - Linear (sequential) search: check each element until you find the target or finish the list. - Aggregations: compute sum, average, count occurrences, or other accumulators while traversing. - Extremes: find minimum or maximum by scanning and updating a running value. - Basic list procedures: APPEND, INSERT(aList, i, value), REMOVE(aList, i), LENGTH(aList), and indexing aList[i] (access/assign). - Simple updates: set aList[i] ← x and use traversal + REMOVE/INSERT to shift elements. Know how to write/evaluate these with the Exam Reference Sheet syntax and remember parallel traversal of multiple lists is out of scope (CED EK AAP-2.O.1). For quick review see the Topic 3.10 study guide (https://library.fiveable.me/ap-computer-science-principles/unit-3/lists/study-guide/mCE6meIGp5pqs1y5ym3h) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-principles).