Algorithms and Programming / LESSON 09

Update lists by tracking positions and length · AP Computer Science Principles

10 min + practice · Original foundation lesson

Access elements by 1-based index, simulate INSERT, REMOVE and APPEND shifts, and trace FOR EACH traversal.

Core ideas and conditions

List indexing is 1-based: the first element is at index 1, a list literal stores values at indices 1, 2, 3 and onward, and valid indices run from 1 to LENGTH(aList). An index below 1 or above the length produces an error and the program terminates, so every access should be checked against the list’s current length. Assignment copies: aList ← bList copies the whole list, and x ← aList[i] copies the value, which does not remove it from the list. Mutation changes length and shifts elements: INSERT(aList, i, value) places value at index i and shifts elements at indices >= i one position right; REMOVE(aList, i) deletes the element at index i and shifts the tail left; APPEND adds at the end without shifting anything.

Worked example: three mutations on one row

Start from items ← [8, 3, 6] and draw the index row 1, 2, 3 under the values. INSERT(items, 1, 5) shifts every element right from the end down to index 1, giving [5, 8, 3, 6] with LENGTH 4. REMOVE(items, 3) deletes the value at index 3, which is 3 after the insert, and shifts 6 left, giving [5, 8, 6] with LENGTH 3. APPEND(items, 12) adds 12 at the end: [5, 8, 6, 12]. The working rule is to re-number the row after each step before consulting the next statement, because earlier indices rarely survive a mutation unchanged.

Traversal and computed indexes

FOR EACH item IN aList runs the block once per element, assigning item to the first element, then the second, and so on to the last, adapting automatically to the list’s current length. The loop variable is a copy: assigning to item inside the block does not write through to the list, so updates must target positions. Indexes may be computed from expressions, as in aList[i + 1], and the validity check applies after the expression is evaluated, not before. When a traversal must skip positions or read two lists at once, an index loop driven by a counter over LENGTH(aList) provides that control instead.

Transfer check: replace or shift

From nums ← [2, 4, 6, 8], a student wants 6 to become 10. Statement A is nums[3] ← 10; statement B is INSERT(nums, 3, 10). Before reading on, write the resulting list and length for each. A replaces in place: [2, 4, 10, 8], still LENGTH 4. B shifts 6 and 8 right and inserts: [2, 4, 10, 6, 8], now LENGTH 5. The difference is overwrite versus shift, and the length change is the tell. Finish by restating, without this example, the condition under which an insertion-style update leaves the length unchanged.

Try it, then explain it.

Complete a short set without looking at the example. After each answer, explain which evidence or rule supports your decision and why the alternatives do not fit.

Practice 3 questions

Sources & authorship

Original OpenExam explanations and examples. References support the concepts; they are not the source of copied questions. Independent educator review is pending.

AP Computer Science Principles Exam Reference Sheet (PDF)