Back to Big Idea 3: Algorithms & Programming

Lesson PreviewLesson 10 of 18 in this unit

3.10 Lists

Part of Big Idea 3: Algorithms & Programming in AP® Computer Science Principles. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Understanding Lists and List Operations

A list is an ordered collection of elements, often used to store multiple values. To access or modify elements in a list, we use indexing. For example, to access the element at position i, we use the syntax aList[i]. The first element of a list is indexed as 1, so aList[1] refers to the first item.

Lists are dynamic structures, allowing programmers to add, remove, or modify elements. For instance, to assign a value of an element in the list to a variable, we use x = aList[i], where x takes the value of the element at index i. Similarly, assigning a value to an element in the list is done using aList[i] = x, which replaces the value at the specified index with x.

To insert an element at a specific index, we use the INSERT function: INSERT(aList, i, value). This function shifts all elements at indices greater than or equal to i to the right, making room for the new element. Conversely, the REMOVE function, REMOVE(aList, i), removes the item at index i and shifts the subsequent elements to the left.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science Principles.