Article
Adding, changing and removing items
A music playlist changes all the time. You add a song at the end, slip a new one into the middle, and delete one you are tired of. A list in a program can change in the same ways. The hard part is knowing what happens to the other items when one is added or removed.
Change, add and remove items
To change one element, put aList[i] on the left of the arrow. To add or remove, the exam gives you three procedures:
| AP pseudocode | What it does | Python |
|---|---|---|
aList[i] ← value | replaces the element at index i | a_list[i - 1] = value |
APPEND(aList, value) | adds value at the end | a_list.append(value) |
INSERT(aList, i, value) | puts value at index i; the old element at i and everything after it shift one place right | a_list.insert(i - 1, value) |
REMOVE(aList, i) | deletes the element at index i; everything after it shifts one place left | a_list.pop(i - 1) |
LENGTH(aList) | the number of elements | len(a_list) |
Follow one list through five steps. The Length column grows and shrinks, and the elements move:
| Step | Statement | The list after it | Length |
|---|---|---|---|
| 0 | a ← [10, 20, 30] | [10, 20, 30] | 3 |
| 1 | APPEND(a, 40) | [10, 20, 30, 40] | 4 |
| 2 | INSERT(a, 2, 15) | [10, 15, 20, 30, 40] | 5 |
| 3 | REMOVE(a, 1) | [15, 20, 30, 40] | 4 |
| 4 | a[2] ← 99 | [15, 99, 30, 40] | 4 |
Here is the same list in Python. Notice the - 1 that turns an AP index into a Python index (the insert(1, 15) is the AP INSERT(a, 2, 15)):
Check your understanding
What is in a after this code runs?
a ← [5, 6] APPEND(a, 7)
What happens to the other indexes
This is the part the exam likes. Look at step 2 closely, INSERT(a, 2, 15):
| Index | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Before | 10 | 20 | 30 | 40 | |
| After | 10 | 15 | 20 | 30 | 40 |
The 15 goes to index 2. The elements that were at index 2 or higher (20, 30, 40) each move one index higher. Nothing is lost, and the length grows by 1.
REMOVE is the opposite. REMOVE(a, 1) deletes the element at index 1 (the 10), and everything after it moves one index lower. The length shrinks by 1.
Two things to remember:
REMOVE(a, 2)removes the element at index 2, not the value 2. (Python'sa.remove(2)is different: it removes the value 2. That is why the table usespop.)INSERTandREMOVEneed an index between 1 andLENGTH(aList). Any other index is an error and the program stops. To add at the end, useAPPEND. You also cannotREMOVEfrom an empty list, because it has no index 1.
Check your understanding
What is displayed when this code runs?
a ← [10, 20, 30] INSERT(a, 2, 15) DISPLAY(a[3])
Copying a list
The exam reference sheet says aList ← bList assigns a copy of bList to aList. After the copy there are two separate lists. Changing one does not change the other:
b ← [1, 2, 3] a ← b APPEND(b, 4) DISPLAY(LENGTH(a))
This displays 3. The list a is a copy made before b grew.
Python works differently here, and it is a trap. a = b does not copy. It gives the same list a second name. Run this:
The first print shows 4, because a and b are one list. The second shows 3, because d = c.copy() made a real copy. (list(c) and c[:] also copy.) On the exam, follow the AP rule: aList ← bList is a copy.
Worked example
What does this display?
letters ← ["a", "b", "c", "d"] REMOVE(letters, 2) INSERT(letters, 1, "z") APPEND(letters, "e") letters[3] ← "y" DISPLAY(letters[3]) DISPLAY(LENGTH(letters))
| Statement | letters after it |
|---|---|
| start | ["a", "b", "c", "d"] |
REMOVE(letters, 2) | ["a", "c", "d"] |
INSERT(letters, 1, "z") | ["z", "a", "c", "d"] |
APPEND(letters, "e") | ["z", "a", "c", "d", "e"] |
letters[3] ← "y" | ["z", "a", "y", "d", "e"] |
The third element is y, and the length is 5. The output is y 5.
Common mistakes
- Removing by value.
REMOVE(a, 3)removes the element at index 3, whatever it is. - Forgetting the shift. After
INSERTorREMOVE, the later elements have new indexes. Redraw the list after each step. - Writing
a ← APPEND(a, 4).APPENDchanges the list itself. It does not give back a new list. - Using the Python index in AP code, or the AP index in Python:
INSERT(a, 2, x)isa.insert(1, x). - Thinking
a = bcopies in Python. It does not. - Using an index outside 1 to
LENGTH(aList). That is an error.
On the exam
Questions show a list and several statements, and ask for the list at the end, a single element, or LENGTH. Redraw the list after every statement and number its indexes from 1. Questions about copies check whether you know that aList ← bList makes a separate list.
Key words
APPEND: adds a value at the end of a list.
INSERT: puts a value at an index and shifts later elements one place right.
REMOVE: deletes the element at an index and shifts later elements one place left.
Copy: a separate list with the same elements.