AP CSP Lists Operations

AP CSP Topics › Lists & Operations

AP CSP Lists: 1-Indexing, Operations & Mutations: Complete Guide (2025‑2026)

A list in AP pseudocode is an ordered collection of items accessed by position number. The critical rule: AP lists are 1-indexed — the first element is at position 1, not 0. The four list operations (APPEND, INSERT, REMOVE, LENGTH) appear on almost every AP CSP exam. APPEND builds new lists; FOR EACH traverses them; LENGTH(list) returns the count.

1Index of the first element in every AP pseudocode list
4Built-in list operations: APPEND, INSERT, REMOVE, LENGTH
0Effect of modifying the FOR EACH loop variable on the original list

1-Based Indexing and Access

AP List Indexing: 1-based (NOT 0-based like Java/Python) scores = 85 92 78 96 71 scores[1] scores[2] scores[3] scores[4] scores[5] = scores[LENGTH(scores)] Key AP Pseudocode List Operations APPEND(list, val) add to end INSERT(list, i, val) insert at position i REMOVE(list, i) remove at position i LENGTH(list) number of elements

AP pseudocode always starts at index 1. scores[1] is 85. scores[LENGTH(scores)] is always the last element.

AP Pseudocode Lists (1-indexed)
What the exam expects
  • scores[1] = first element
  • scores[LENGTH(scores)] = last element
  • Index 0 does not exist
  • LENGTH([10,20,30]) = 3
  • Exam always uses 1-based indexing
Java/Python (0-indexed)
Do NOT apply on the AP exam
  • scores[0] = first element in Java
  • scores[-1] = last element in Python
  • Off by one if you use 0-index habits
  • Java: scores.length (no parentheses)
  • Python: len(scores) (different syntax)

The Four List Operations

All Four Operations With Examples
myList [10, 20, 30] APPEND(myList, 40) # myList = [10,20,30,40] INSERT(myList, 2, 15) # myList = [10,15,20,30,40] REMOVE(myList, 3) # myList = [10,15,30,40] DISPLAY(LENGTH(myList)) # 4

Code Trace Gauntlet

Trace 1 — APPEND in a Loop
evens [] nums [1,2,3,4,5,6] FOR EACH n IN nums IF n MOD 2 = 0 APPEND(evens, n) DISPLAY(evens)

What does evens contain after the loop?

Output

[2, 4, 6] 2 MOD 2=0 T, 4 MOD 2=0 T, 6 MOD 2=0 T. Odd numbers (1,3,5) fail the condition.

Trace 2 — Index Access and LENGTH
data [7, 14, 21, 28] DISPLAY(data[1]) DISPLAY(data[LENGTH(data)]) DISPLAY(LENGTH(data))

What are the three outputs?

Output

7 28 4 data[1]=7 (first). data[LENGTH(data)]=data[4]=28 (last). LENGTH(data)=4.

Trace 3 — INSERT Shifts Elements
lst [1, 2, 3] INSERT(lst, 2, 99) DISPLAY(lst[2]) DISPLAY(lst[3])

INSERT at position 2. What do the two DISPLAYs output?

Output

99 2 INSERT(lst,2,99) inserts 99 at index 2, shifting old elements right: [1,99,2,3]. lst[2]=99, lst[3]=2.

Trace 4 — REMOVE and Reindexing
lst [“a”,“b”,“c”,“d”] REMOVE(lst, 2) DISPLAY(lst[2]) DISPLAY(LENGTH(lst))

After REMOVE(lst,2), what is at position 2 and what is the new length?

Output

c 3 Removing index 2 (b) shifts remaining elements left: [a,c,d]. New lst[2]=c. LENGTH=3.

Trace 5 — Find Maximum Value
vals [5,12,3,18,7] maxV vals[1] FOR EACH v IN vals IF v > maxV maxV v DISPLAY(maxV)

What is the maximum value found?

Output

18 maxV starts at 5. v=5:no. v=12>5:maxV=12. v=3:no. v=18>12:maxV=18. v=7:no. Display:18.

Spot the Bug

SPOT THE BUG — Zero-Based Index Error
colors [“red”,“blue”,“green”] DISPLAY(colors[0])
Bug Explained

AP lists are 1-indexed. colors[0] is an error. Fix: colors[1] returns "red".

SPOT THE BUG — Wrong Max Initialization
nums [5,3,8,2] maxV 0 FOR EACH v IN nums IF v > maxV maxV v
Bug Explained

Works for positive lists, fails for all-negative lists (e.g. [-3,-1,-5] returns 0). Fix: initialize maxV ← nums[1] to use the first element.

Common Exam Pitfalls

1
AP lists start at index 1, not 0

scores[1] is the first element. scores[0] does not exist. Every AP exam question uses 1-based indexing.

2
INSERT and REMOVE shift all subsequent elements

INSERT(lst,2,x) pushes everything at position 2+ one slot right. REMOVE(lst,2) pulls everything at position 3+ one slot left. The list renumbers.

3
FOR EACH loop variable is a copy — modifying it does nothing to the list

n ← n*2 inside a FOR EACH loop does not double the list elements. Build a new list with APPEND if you need transformed values.

4
LENGTH returns the count, and the last index equals LENGTH

For a list of 4 elements, LENGTH=4 and the last valid index is also 4. list[LENGTH(list)] is always the last element.

Check for Understanding

1. lst ← [10,20,30,40,50]
DISPLAY(lst[3])

  • 20
  • 30
  • 40
  • 3
AP lists are 1-indexed. lst[3] is the third element: 30.

2. lst ← ["x","y","z"]
APPEND(lst,"w")
DISPLAY(LENGTH(lst))

  • 3
  • 4
  • w
  • 1
APPEND adds one element. 3+1=4.

3. lst ← [1,2,3,4]
INSERT(lst,2,9)
DISPLAY(lst[3])

  • 9
  • 2
  • 3
  • 1
INSERT(lst,2,9) inserts 9 at index 2, shifting others right: [1,9,2,3,4]. lst[3]=2.

4. lst ← ["a","b","c","d"]
REMOVE(lst,1)
DISPLAY(lst[1])

  • a
  • b
  • c
  • Error
Removing index 1 (a) shifts remaining elements left: [b,c,d]. New lst[1]=b.

5. nums ← [3,1,4,1,5]
total ← 0
FOR EACH n IN nums
total ← total + n
DISPLAY(total)

  • 10
  • 14
  • 5
  • 15
3+1+4+1+5=14.

6. Which correctly returns the last element of any list?

  • list[0]
  • list[LENGTH(list)-1]
  • list[LENGTH(list)]
  • list[LENGTH(list)+1]
In 1-indexed AP pseudocode, the last element index equals LENGTH(list).

7. Consider: I. APPEND always adds to the end. II. INSERT(lst,1,x) inserts x before all existing elements. III. REMOVE(lst,2) removes the element at index 2 and shifts subsequent elements left. Which are correct?

  • I only
  • I and III only
  • I, II, and III
  • II and III only
All three are correct. APPEND adds to end; INSERT(lst,1,x) pushes everything right making x the new first element; REMOVE shifts left.

8. lst ← [5,10,15]
FOR EACH n IN lst
n ← n * 2
DISPLAY(lst)


What does DISPLAY output?

  • [10,20,30]
  • [5,10,15]
  • An error
  • 30
Modifying n does not change lst. The loop variable is a copy. lst remains [5,10,15].

9. lst ← [2,4,6,8]
DISPLAY(lst[LENGTH(lst)])

  • 2
  • 4
  • 6
  • 8
LENGTH(lst)=4. lst[4]=8, the last element.

10. A procedure builds a list of all values greater than 10. The list starts empty. Which line correctly adds qualifying values?

  • INSERT(result, n)
  • APPEND(result, n)
  • result[n] ← n
  • result ← result + n
APPEND(result, n) adds n to the end of the result list. INSERT requires an index. The other options use incorrect syntax.

How the AP Exam Tests This

  • Access a specific element by index (always 1-based) and report its value
  • Trace APPEND inside a FOR EACH loop and report the resulting list
  • Determine the effect of INSERT or REMOVE on element positions
  • I/II/III: which statements about list operations are correct
  • Identify the correct operation to add an element to the end of a list

FAQ

Can a list contain different types?
Yes. AP pseudocode lists can contain numbers, strings, or booleans in the same list. The exam generally uses homogeneous lists for clarity.
What is the difference between APPEND and INSERT?
APPEND(list, val) always adds to the end. INSERT(list, i, val) inserts at a specific position and shifts everything at i and beyond one slot right.

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]