PrepGo

Lists - AP Computer Science Principles Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 15 minutes to read.

Getting Started

Imagine trying to manage a grocery list, a list of contacts on your phone, or a playlist of your favorite songs. In each case, you have a collection of related items that have a specific order. To manage such collections efficiently in a program, we need a way to store and organize them, which is where one of computing's most fundamental structures, the list, becomes essential.

What You Should Be able to Do

  • Explain the purpose of a list and how it stores an ordered sequence of data.

  • Write expressions to access and modify specific elements within a list using their position, or index.

  • Design an algorithm that traverses a list to process each of its elements.

  • Trace the execution of an algorithm that creates and manipulates a list to determine its final state.

Key Concepts & Application

The Core Idea

A list is an ordered collection of elements. Think of it like a numbered series of boxes, where each box can hold one piece of information (an element). The number on each box is its index, which tells you its position in the sequence. The first element has an index of 1, the second has an index of 2, and so on. This structure allows you to store multiple related values under a single variable name.

For example, instead of creating separate variables like score1, score2, and score3, you can create a single list called scores that holds all three values. This is a powerful form of Data Abstraction, which is the practice of simplifying complex data into a manageable structure. By using a list, we can focus on what we want to do with the collection of scores (like find the average) without worrying about the low-level details of how the computer stores each individual value in memory.

Logic & Application

Working with lists involves a standard set of operations for creating, reading, and changing the data they hold. An algorithm is a finite set of instructions that accomplishes a specific task, and many algorithms are designed specifically to process data stored in lists.

Key Principles of Lists

  • Indexing: Every element has a unique index, a number representing its position. In the pseudocode used for AP exams, indexing starts at 1.

  • Ordered: The elements in a list maintain their order unless explicitly changed. The element at index 1 will always come before the element at index 2.

  • Dynamic Size: Lists can grow or shrink. You can add new elements or remove existing ones.

Annotated Pseudocode Examples

1. Creating a List and Accessing Elements

You can create a list and then access individual elements using their index inside square brackets [].


// Create a list of high scores

highScores <- [105, 98, 110, 85]


// Access the element at index 3 (the third element)

thirdScore <- highScores[3]  // thirdScore is now 110


// Display the first element in the list

DISPLAY(highScores[1])       // Displays 105

2. Modifying a List

You can change the value at an existing index or use procedures to add or remove elements, which can change the list's length.


grades <- [88, 75, 91]


// Change the value at index 2

grades[2] <- 80              // grades is now [88, 80, 91]


// Add a new element to the end of the list

APPEND(grades, 95)           // grades is now [88, 80, 91, 95]


// Insert an element at a specific position (index 2)

INSERT(grades, 2, 85)        // grades is now [88, 85, 80, 91, 95]


// Remove the element at index 3

REMOVE(grades, 3)            // grades is now [88, 85, 91, 95]

3. Traversing a List with a Loop

Iteration is the process of repeating a set of instructions. A common and powerful use of lists is to iterate, or "traverse," through them, performing an action on each element. The FOR EACH loop is perfect for this.


// Calculate the sum of all numbers in a list

prices <- [10.50, 4.00, 8.25]

totalCost <- 0


// The variable 'item' will hold each element from the list, one by one

FOR EACH item IN prices

{

  // Add the current item's value to totalCost

  totalCost <- totalCost + item

}


DISPLAY(totalCost) // Displays 22.75

Tracing & Analysis

Tracing code helps you understand how an algorithm works step-by-step. Let's trace the traversal example from above.

Logic Trace: Calculating totalCost

Iterationitem (Current Element)totalCost (Before Addition)totalCost (After Addition)prices List (Unchanged)
110.50010.50[10.50, 4.00, 8.25]
24.0010.5014.50[10.50, 4.00, 8.25]
38.2514.5022.75[10.50, 4.00, 8.25]
End of Loop--22.75[10.50, 4.00, 8.25]

The final value of totalCost is 22.75. This systematic process of iterating through a list is a foundational concept in programming and data analysis.

Key Terminology & Logic

This table summarizes the core pseudocode operations for managing lists.

PseudocodePurpose
aList[i]Accesses the element of aList at index i.
aList[i] <- valueAssigns value to the element of aList at index i.
APPEND(aList, value)Adds value to the end of aList, increasing its length by one.
INSERT(aList, i, value)Inserts value into aList at index i, shifting other elements.
REMOVE(aList, i)Removes the element at index i from aList, shifting other elements.
LENGTH(aList)Evaluates to the number of elements in aList.
FOR EACH item IN aListA loop structure that iterates through each element of aList.

Core Concepts & Terminology

  • List: An ordered collection of elements, where each element can be accessed by an index.

  • Element: A single value or item stored within a list.

  • Index: A number representing the position of an element in a list. Exam reference sheet pseudocode uses 1-based indexing.

  • Iteration: The process of repeating a set of instructions, often used to "visit" every element in a list one by one.

  • Data Abstraction: The process of hiding complex implementation details and showing only the essential features. A list is a data abstraction because we use it without needing to know how it is stored in memory.

  • List Traversal: The process of accessing each element in a list, typically with a loop, to perform some operation.

    
    numbers <- [10, 20, 30]
    
    FOR EACH num IN numbers
    
    {
    
      DISPLAY(num)
    
    }
    

    This algorithm iterates through the numbers list and displays each element in order.

Core Skill Check

  • Logic Tracing: What is the final value of count after this pseudocode runs?

    data <- [5, 10, 15, 20]; count <- 0; FOR EACH item IN data { IF (item > 10) { count <- count + 1 } }

  • Debugging: Identify the logic error in this pseudocode, which is intended to display the last element of a list:

    myList <- [2, 4, 6]; lastItemIndex <- LENGTH(myList) + 1; DISPLAY(myList[lastItemIndex])

  • Application: Describe a real-world scenario where a list would be a more appropriate data structure than several individual variables.

Common Misconceptions & Clarifications

  • "List indexes always start at 0."

    • While true in many programming languages like Java and Python, the pseudocode on the AP exam uses 1-based indexing, where the first element is at index 1.
  • "A list can only hold one type of data, like only numbers."

    • Lists are flexible and can store elements of different data types, including numbers, strings, and booleans, sometimes even in the same list.
  • "Removing an element from the middle of a list leaves an empty gap."

    • When an element is removed, the list automatically adjusts. Elements after the removed one shift to the left to fill the space, and the list's total length decreases by one.
  • "The FOR EACH loop gives you the index of each item."

    • The FOR EACH item IN aList loop gives you direct access to the value of each element (item), not its index. If you need the index, a different style of loop is required.

Summary

Lists are a fundamental and powerful tool in programming for managing ordered collections of data. They allow us to group related information under a single variable, simplifying our code and making it easier to manage. By using operations to access, modify, and iterate through list elements, we can write sophisticated algorithms to process large amounts of information efficiently. Understanding how to use lists is a critical step in moving from working with single data values to managing complex data sets.