APrep Open the app

AP Computer Science Principles · Unit 3: Algorithms & Programming

3.10 Lists

What is a List?

A list is a data structure that stores an ordered sequence of elements, referenced by a single name. Each element has an index indicating its position, and in AP CSP pseudocode, indexing typically starts at 1. Lists can hold numbers, strings, or other data, and their length can change as elements are added or removed.

List Operations

Common operations include: accessing an element by index (`list[i]`), changing an element's value, APPEND (adds an item to the end, increasing length by 1), INSERT (adds an item at a specific index), and REMOVE (deletes an item at a specific index). These operations let programs dynamically manage collections of data.

Traversing Lists

A traversal processes each element of a list once, usually with a loop. This is essential for tasks like searching for a value, computing a sum, or applying an operation to every item. Combining loops with list indices lets programmers efficiently process large amounts of data without writing repetitive code.

Key terms

List
An ordered sequence of elements stored under a single variable name, each accessible by index.
Index (in AP CSP pseudocode)
The position of an element in a list; indexing starts at 1.
APPEND(list, item)
Adds item to the end of the list, increasing its length by 1.
INSERT(list, i, item)
Adds item at index i, shifting subsequent elements to later positions.
List traversal
Processing each element of a list once, typically using a loop, to search, modify, or analyze data.

Try an AP-style question

Code

nums ← [10, 20, 30, 40]
DISPLAY(nums[2])

What is displayed?

  1. 20
  2. 30
  3. An error
  4. 10
Show the answer

A. AP lists start at index 1.

Practice 8 AP-style questions with instant explanations, flashcards and spaced review.

Study 3.10 free in APrep