List Operations (College Board AP® Computer Science Principles): Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

List operations & syntax

What operations can be performed on a list?

  • A list stores an ordered collection of elements that can be accessed, modified and managed using specific operations

  • Lists can also be referred to as arrays

  • The AP exam uses 1-based indexing, so the first element in a list is at index 1

  • Accessing an index less than 1 or greater than the length of the list produces an error

Operation

Exam notation

Description

Empty list

aList ← []

Create a list with no elements

Access by index

aList[i]

Retrieves the element at position i in the list

Assign to element

aList[i] ← value

Replaces the element at position i with a new value

Assign list to variable

aList ← [value1, value2, value3]

Creates a list with the specified values

INSERT

INSERT(aList, i, value)

Inserts value at position i, shifting all elements after i one position to the right, increasing list length by 1

APPEND

APPEND(aList, value)

Adds value to the end of the list, increasing list length by 1

REMOVE

REMOVE(aList, i)

Removes the element at position i, shifting all elements after i one position to the left, decreasing list length by 1

LENGTH

LENGTH(aList)

Returns the number of elements currently in the list

Copy list

aList ← bList

Copy the content of one list to another

How INSERT, APPEND and REMOVE change a list

aList ← ["a", "b", "c"]
 
  • INSERT(aList, 2, "x") produces ["a", "x", "b", "c"] (inserts at position 2, pushes "b" and "c" right)

  • APPEND(aList, "d") produces ["a", "x", "b", "c", "d"] (adds to the end)

  • REMOVE(aList, 1) produces ["x", "b", "c", "d"] (removes "a", everything shifts left)

List iteration methods

How are lists traversed in a program?

  • A traversal is the process of accessing each element in a list one at a time

  • A complete traversal visits every element in the list from start to finish

  • A partial traversal stops early once a condition is met, without visiting every element

FOR EACH loops

  • The FOR EACH loop is designed specifically for traversing lists

  • It automatically accesses each element in order without needing to manage an index variable

scores ← [85, 92, 78, 95]
total ← 0
FOR EACH score IN scores
{
   total ← total + score
}
average ← total / LENGTH(scores)
DISPLAY(average)
 
  • Output: 87.5

Common list algorithms

  • These are standard patterns that appear frequently on the AP exam

Algorithm

Purpose

How it works

Find minimum/maximum

Identify the smallest or largest value in a list

Set a variable to the first element, then compare each remaining element and update if smaller/larger

Compute sum/average

Calculate the total or mean of all values

Use a running total variable, add each element, divide by LENGTH for the average

Linear search

Determine whether a value exists in a list

Check each element one at a time until the value is found or the end of the list is reached

Examiner Tips and Tricks

  • INSERT and REMOVE are the most commonly tested list operations because they change the positions of other elements. When tracing code that uses INSERT, remember that elements at and after the insertion point shift right. When REMOVE is used, elements after the removed position shift left. Always track the list length as it changes.

  • For the AP Create Performance Task, your program must include a list that manages complexity — on exam day be prepared to explain how your list stores data and how accessing or modifying its elements contributes to your program's purpose

Worked Example

A program contains the following code segment.

animals ← ["cat", "dog", "bird"]
INSERT(animals, 2, "fish")
REMOVE(animals, 4)
DISPLAY(animals)
 

What is displayed as a result of executing the code segment?

(A) ["cat", "fish", "dog"]

(B) ["cat", "fish", "dog", "bird"]

(C) ["cat", "fish", "bird"]

(D) ["cat", "dog", "fish"]

[1]

Answer:

(A) ["cat", "fish", "dog"] [1 mark]

  • INSERT at position 2 shifts "dog" and "bird" right to produce ["cat", "fish", "dog", "bird"]; REMOVE at position 4 then removes "bird", leaving ["cat", "fish", "dog"]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.