zuai-logo

Lists

David Foster

David Foster

7 min read

Listen to this study note

Study Guide Overview

This study guide covers list operations in AP Computer Science Principles, focusing on basic operations, adding and removing elements, and looping. Key concepts include accessing elements by index, assigning values, inserting/appending/removing elements, determining list length, complete/partial traversal using for and while loops, and linear search. It also provides exam tips, focusing on common question types and high-priority topics like list traversal and modification, along with practice questions covering these concepts.

AP Computer Science Principles: List Operations - The Night Before 🚀

Hey! Let's get you prepped for the AP CSP exam with a super-focused review of list operations. We'll make sure you're not just memorizing, but understanding how lists work. Let's dive in!

Basic List Operations

Lists are fundamental in programming, and they're a big deal on the AP exam. Remember, AP Pseudocode starts indexing at 1, but in most programming languages like Python, it starts at 0. Keep this in mind to avoid common mistakes!

Key Concept

Accessing Elements by Index

Think of a list like a set of numbered mailboxes. To get something from a specific box, you use its number (the index).

markdown-image

  • How it works: list_name[index]
  • Example:

python grocery_list = ["milk", "eggs", "cheese"] print(grocery_list[0]) # Output: milk


### 
<div data-custom-tag="key_point"> 

Assigning Element Values

</div>

#### Assigning to a Variable

![](https://zupay.blob.core.windows.net/resources/files/0baca4f69800419293b4c75aa2870acd_c6ae50_520.png?alt=media&token=a16b04a1-200a-41bd-a044-91f4619284dd)

- You can grab an element from a list and store it in a variable.
- **Example:**

python grocery_list = ["milk", "eggs", "cheese"] change = "soap" grocery_list[2] = change print(grocery_list) # Output: ["milk", "eggs", "soap"]


#### Assigning a Value Directly

![](https://zupay.blob.core.windows.net/resources/files/0baca4f69800419293b4c75aa2870acd_9c2d56_1975.png?alt=media&token=5e5ce976-66a5-4657-ba7b-f56206ff0191)

- You can directly change the value of an element at a specific index.
- **Example:**

python grocery_list = ["milk", "eggs", "cheese"] grocery_list[2] = "fish" print(grocery_list) # Output: ["milk", "eggs", "fish"]


#### Assigning One Element to Another

![](https://zupay.blob.core.windows.net/resources/files/0baca4f69800419293b4c75aa2870acd_893a7c_1055.png?alt=media&token=5040c704-7717-4ef9-a3c5-...

Question 1 of 11

Given a list fruits = ['apple', 'banana', 'cherry'], what is the output of print(fruits[1])? 🍎

apple

banana

cherry

None