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

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

List fundamentals

What is a list?

  • A list is a data structure that allows multiple values to be stored in a single variable

  • Lists can also be referred to as an array

  • A list is an ordered sequence, meaning its elements are arranged in a specific order that is maintained

  • Each element holds a current value that can be read or updated

  • Assignment to a list element works the same way as with single variables: a value can be assigned to a specific position

scores ← [85, 92, 78, 90]
scores[2] ← 80
 
  • In this example, the element at index 2 is updated from 92 to 80 (the current value at that position is replaced)

Indices and index references

  • Each element has a position identified by an index (also called an index reference)

  • In AP CSP pseudocode, list indices start at 1 (the first element is at index 1, the second at index 2, and so on)

  • Indices allow programs to access, update, or remove specific elements without affecting the rest of the list

  • Each index position is unique: no two elements share the same index

names ← ["Alex", "Jordan", "Sam"]
DISPLAY(names[1])
 
  • This displays "Alex" because index 1 refers to the first element in the list

List properties and abstraction

What properties do lists have?

  • Lists can contain mixed element types: a single list can hold numbers, strings, and Booleans together

  • Grouped items in a list share a common purpose or context (for example, a list of test scores or a list of student names)

  • List initialization is the process of creating a list and assigning its starting values

  • A list can also be initialized as empty and have elements added during program execution

Property

Description

Example

Mixed types

A list can hold values of different data types

["Alex", 16, true]

Grouped items

Elements are related by a shared purpose

A list of all scores from one test

Initialization

A list is created with or without starting values

scores ← [] or scores ← [85, 90]

Lists as abstraction

  • A list is a form of list abstraction because it allows a programmer to work with a named collection of related values as a single unit

  • Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation

  • Programmers work with a list through its name and indices, without needing to know how it is stored in memory

  • Using lists simplifies development by reducing the number of variables needed and keeping related data together

  • Lists also improve maintenance: if the data structure needs to change, updates are made in one place rather than across many separate variables

Index management

How are indices managed in a list?

  • The index range of a list runs from 1 to the length of the list in AP CSP pseudocode

  • Accessing an index outside this range (below 1 or above the list length) causes an error because the position is out of bounds

  • Programs must account for bounds when adding, removing, or accessing elements to avoid runtime errors

  • When elements are inserted or removed, the indices of subsequent elements shift accordingly

colors ← ["red", "green", "blue"]
DISPLAY(colors[4])
 
  • This causes an error because the list only contains 3 elements, so index 4 is out of bounds

Examiner Tips and Tricks

  • AP CSP pseudocode uses 1-based indexing — the first element is at index 1, not index 0. This is a common source of errors on the exam, especially if you are used to programming languages that start at 0. When tracing code that modifies a list, track which elements shift position after an insertion or removal.

  • For the AP Create Performance Task, you must include a list in your program — demonstrate that you can store, access, and process list elements meaningfully.

Worked Example

Consider the following code segment.

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

What is displayed when this code segment is executed?

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

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

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

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

[1]

Answer:

(B) ["cat", "bird", "fish"] [1 mark]

  • animals[2] ← "bird" replaces "dog" at index 2 with "bird"; the elements at indices 1 and 3 are unaffected

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.