A list stores an ordered sequence of values under one name. In the AP® Computer Science Principles exam pseudocode, the first element is at index 1 and the last is at index LENGTH(aList). The list procedures APPEND, INSERT and REMOVE change the length, the last two shift every later element by one place, and any index below 1 or above the length is an error that stops the program.
Quick check
After x ← 5 and then x ← x + 2, what value does x hold?Show answerHide answer
- The second line reads the old value 5, adds 2 and stores the result back in
x.
- The second line reads the old value 5, adds 2 and stores the result back in
A list holds 4 values, numbered from 1. What is the number of the last one?Show answerHide answer
- Counting from 1, the last position always equals the number of values.
If you squeeze a new value into position 2 of a list, what happens to the value that was there?Show answerHide answer
It moves up to position 3, and every value after it moves up one place too.
Know these already? Jump to the practice questions
What you'll be able to do
- State the index of any element in a list that starts at index 1.
- Predict a list's contents after
APPEND,INSERTandREMOVE. - Calculate
LENGTHafter a sequence of list operations. - Identify an index that causes an error.
Key terms
List
An ordered sequence of elements stored under one variable name.
- In plain English
- A numbered row of boxes with one label for the whole row.
- Don't write
- "A list is a set of variables called list1, list2 and list3."
Element
One value stored in a list.
- In plain English
- What is inside one box.
- Don't write
- "The element is the position number."
Index
A whole number that gives an element's position; in the exam pseudocode it runs from 1 to the list's length.
- In plain English
- The box's number, counting from 1.
- Don't write
- "The first element is at index 0."
How do you create a list and read an element?
You create a list with square brackets, and aList ← [] makes an empty list. aList[i] is the element at index i.
scores ← [70, 85, 92]
| Index | 1 | 2 | 3 |
|---|---|---|---|
| Element | 70 | 85 | 92 |
scores[1] is 70 and scores[3] is 92. LENGTH(scores) is 3, the number of elements, so scores[LENGTH(scores)] is always the last element, whatever the list's size. x ← scores[2] copies the value 85 into x.
Creating lists, aList[i] with indexes from 1, and LENGTH are all on the exam reference sheet.
Key idea
The first index is 1 and the last index equals LENGTH(aList).
Check yourself
Show answerHide answer
LENGTH(pets)is 4, and the element at index 4 is 7.
How do you change an element?
Assign to the element: scores[2] ← 88 replaces 85 with 88, giving [70, 88, 92]. The length stays 3, because replacing never adds or removes a place.
The right-hand side is worked out first, using the old values, then stored. So scores[1] ← scores[1] + scores[3] adds 70 and 92 and stores 162 at index 1.
The simplest way to swap two elements uses a third variable. Copy one element into it before you overwrite that element; otherwise its value is lost.
Check yourself
Show answerHide answer
[3, 6, 6]. vals[1] * 2 is 6, which replaces the 9 at index 3.
What do APPEND, INSERT and REMOVE do?
All three change the length, and two of them shift later elements. Start with c ← [10, 20, 30, 40]:
| Call | What it does | c afterwards | Length |
|---|---|---|---|
INSERT(c, 2, 15) | elements at index 2 and after move up one; 15 goes in at index 2 | [10, 15, 20, 30, 40] | 5 |
REMOVE(c, 1) | element at index 1 goes; later elements move down one | [15, 20, 30, 40] | 4 |
APPEND(c, 50) | 50 goes on the end | [15, 20, 30, 40, 50] | 5 |
The shift changes indexes, not just values. After REMOVE(c, 1), the 20 that was at index 3 is at index 2. All four list procedures are on the exam reference sheet.
Check yourself
Show answerHide answer
[5, 9, 6, 7], with length 4. The 6 and 7 moved up one place.
Why is an index outside 1 to LENGTH an error?
There is no element there to read, change, insert at or remove. For a list of length 3, only indexes 1, 2 and 3 exist, so aList[0] and aList[4] are both errors, and the program stops. That rule is Learn this.
Watch the length as it changes. After a REMOVE, the old last index no longer exists, so code that was safe a moment ago can fail.
Check yourself
Show answerHide answer
No. q is now [1], with length 1, so index 2 is past the end.
Worked example 1: Trace list operations
Question: What is displayed when this code segment runs?
nums ← [4, 7, 1]
APPEND(nums, 9)
INSERT(nums, 2, 5)
REMOVE(nums, 4)
DISPLAY(nums[3])
DISPLAY(LENGTH(nums))
What tells you the method
Each line changes the list, so write out the whole list after every line.
Step 1: Trace the list.
Line numsLength nums ← [4, 7, 1][4, 7, 1] 3 APPEND(nums, 9)[4, 7, 1, 9] 4 INSERT(nums, 2, 5)[4, 5, 7, 1, 9] 5 REMOVE(nums, 4)[4, 5, 7, 9] 4 WhyINSERTpushed 7, 1 and 9 up one place, so index 4 now holds 1, not 9.Step 2: Read the displays.
nums[3]is 7 and the length is 4, shown on one line.WhyEach
DISPLAYshows its value followed by a space.
Check it
The list gained two elements and lost one, so its length is 3 + 2 − 1 = 4.
Answer
7 4
Worked example 2: Design a "move to back" step
Question: A queue holds ticket numbers in tickets ← [12, 30, 45]. Design steps that move the first ticket to the back, then state what DISPLAY(tickets[1]) followed by DISPLAY(tickets[3]) shows.
What tells you the method
A design task: state the input, the output and the steps, then trace to test.
Step 1: State the input, output and plan.
Input: [12, 30, 45]. Output: [30, 45, 12]. Plan: save the first element, remove it, append the saved value.
WhyREMOVEdeletes the value, so it must be saved first.Step 2: Write the steps.
first ← tickets[1] REMOVE(tickets, 1) APPEND(tickets, first)WhyAfter
REMOVE, 30 and 45 shift down to indexes 1 and 2, soAPPENDputs 12 at index 3.Step 3: Trace it.
firstis 12;ticketsbecomes [30, 45], then [30, 45, 12]. The displays show 30 and 12.WhyTracing confirms the design before you rely on it.
Check it
The length is 3 before and after, so nothing was lost. Saving after the REMOVE would store 30 and give [30, 45, 30].
Answer
30 12
Worked example 3: Choose the code that swaps
Question: vals ← [8, 3, 6]. Which code segment changes vals to [3, 8, 6]?
- A)
vals[1] ← vals[2], thenvals[2] ← vals[1] - B)
temp ← vals[1], thenvals[1] ← vals[2], thenvals[2] ← temp - C)
temp ← vals[1], thenvals[2] ← temp, thenvals[1] ← vals[2] - D)
temp ← vals[0], thenvals[0] ← vals[1], thenvals[1] ← temp
What tells you the method
"Which code segment" means trace each option on the given list.
Step 1: Trace A and C.
A: [3, 3, 6], because 8 was overwritten before it was saved. C:
vals[2]becomes 8 first, sovals[1]copies 8: [8, 8, 6].WhyOrder matters; a value must be saved before it is overwritten.
Step 2: Trace B and D.
B:
tempis 8, then [3, 3, 6], then [3, 8, 6]. D readsvals[0], which is an error.WhyIndexes start at 1, so index 0 does not exist.
Check it
B keeps the same three values, only reordered, as a swap should.
Answer
B
Your turn
Question: What is displayed when this code segment runs?
pets ← [3, 9, 2]
INSERT(pets, 1, 6)
pets[3] ← pets[3] + pets[4]
DISPLAY(pets[3])
DISPLAY(LENGTH(pets))
Step 1: Trace the INSERT.
6 goes in at index 1 and the rest move up: [6, 3, 9, 2].
Step 2: Your go: work out the new
pets[3], then the two displays.
Show the full solutionHide the solution
Step 2. pets[3] is 9 and pets[4] is 2, so pets[3] becomes 11: [6, 3, 11, 2]. The length is 4.
Answer: 11 4
Traps
Trap
The first element is at index 0.
It seems right because many programming languages count from 0. But the exam pseudocode counts from 1, so aList[1] is the first element and aList[0] is an error.
For a ← [10, 20, 30], what is a[1]?
10, the first element.
Trap
After REMOVE, the other elements keep their indexes.
It seems right because removing one value looks like leaving a gap. But lists have no gaps: every later element moves down one index.
After m ← ["a", "b", "c"] and REMOVE(m, 1), what is m[1]?
"b". It moved down from index 2 to index 1.
Trap
INSERT(aList, i, value) replaces the element at index i.
It seems right because assignment to aList[i] does replace. But INSERT keeps every element and pushes the ones from index i onward up a place, so the length grows by 1.
After a ← [5, 6, 7] and INSERT(a, 2, 9), is 6 still in the list?
Yes. It moved to index 3: [5, 9, 6, 7].
What's examined
On the AP® Computer Science Principles exam, lists are part of Big Idea 3: Algorithms and Programming. Multiple-choice questions ask what a list holds or what is displayed after a sequence of list operations, and which code segment produces a given list. Your Create program must also use a list that helps manage complexity. Going through a list with a loop is in the note on traversing lists.
Practice questions
- Recall Which expression is the last element of a non-empty list
aList?
- A)
aList[LENGTH(aList)] - B)
aList[LENGTH(aList) - 1] - C)
aList[LENGTH(aList) + 1] - D)
LENGTH(aList)
Show answer
Answer: A. Indexes run from 1 to the length, so the last index equals LENGTH(aList).
Why the others are wrong:
- B uses the count-from-0 habit and gives the second-to-last element.
- C is one past the end, so it is an error.
- D is the number of elements, not an element.
- Apply What is displayed when this code segment runs?
w ← [6, 2, 8, 5]
w[2] ← w[1] * w[4]
REMOVE(w, 1)
APPEND(w, w[1] - 1)
DISPLAY(w[2])
DISPLAY(w[LENGTH(w)])
Show answer
Answer: 8 29
Marking points:
w[2]becomes 6 × 5 = 30: [6, 30, 8, 5].REMOVE(w, 1)gives [30, 8, 5]; then 30 − 1 = 29 is appended: [30, 8, 5, 29].w[2]is 8 and the last element is 29.
- Apply
data ← [5, 10, 15, 20, 25]. What doesdatahold afterREMOVE(data, 2)and thenREMOVE(data, 4)?
- A) [5, 15, 25]
- B) An error, because the list no longer has an index 4
- C) [5, 10, 20]
- D) [5, 15, 20]
Show answer
Answer: D. The first call gives [5, 15, 20, 25]; index 4 is now 25, so the second call removes 25.
Why the others are wrong:
- A assumes the indexes did not shift after the first
REMOVE. - B miscounts the new length: 4 elements remain, so index 4 exists.
- C counts indexes from 0 in the original list and ignores the shift, removing 15 and 25.
- Exam-style Give the number of the line where this program stops with an error.
1: q ← [2, 4, 6]
2: REMOVE(q, 1)
3: APPEND(q, 8)
4: q[3] ← q[1] + q[2]
5: REMOVE(q, 2)
6: DISPLAY(q[3])
Show answer
Answer: Line 6.
Marking points:
- After line 4,
qis [4, 6, 10]; line 5 gives [4, 10]. qnow has length 2, soq[3]on line 6 is past the end.
- Apply After these lines run, at what index is the value 30?
nums ← [30, 50]
INSERT(nums, 1, 10)
INSERT(nums, 2, 20)
APPEND(nums, 60)
Show answer
Answer: 3
Marking points:
- The list becomes [10, 30, 50], then [10, 20, 30, 50], then [10, 20, 30, 50, 60].
- Exam-style After this code segment runs, what are (a)
LENGTH(m)and (b)m[3]?
m ← []
APPEND(m, 3)
APPEND(m, 1)
INSERT(m, 1, 7)
APPEND(m, 4)
REMOVE(m, 2)
INSERT(m, 3, 0)
Show answer
Answer: (a) 4 (b) 0
Marking points:
- The list goes [3], [3, 1], [7, 3, 1], [7, 3, 1, 4], [7, 1, 4], then [7, 1, 0, 4].
- Exam-style
nums ← [1, 3]. Which call changesnumsto [1, 2, 3]?
- A)
APPEND(nums, 2) - B)
INSERT(nums, 1, 2) - C)
INSERT(nums, 2, 2) - D)
nums[2] ← 2
Show answer
Answer: C. The 3 at index 2 moves up to index 3, and 2 goes in at index 2.
Why the others are wrong:
- A puts 2 at the end: [1, 3, 2].
- B inserts at index 1, before the 1: [2, 1, 3].
- D replaces the 3 instead of inserting: [1, 2].
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- In the exam pseudocode, valid indexes run from 1 to
LENGTH(aList); anything else stops the program. - Assigning to
aList[i]replaces a value and keeps the length. INSERTpushes later elements up andREMOVEpulls them down, so indexes change.- Save a value in another variable before you overwrite or remove it.
- Trace list code by writing the whole list after every line.