Table of contents |
|
Introduction |
|
Basic List Operations |
|
Adding and Removing Elements |
|
Looping Through Lists |
|
Key Terms |
|
Lists are a key concept in AP Computer Science Principles, used to store and manage multiple items in a specific order. This chapter explains how to work with lists, including accessing, changing, adding, and removing elements. It also covers how to find the length of a list and how to loop through its elements. Understanding lists helps programmers organize data efficiently and perform operations like searching or sorting, which are essential for many programs.
The AP exam tests your ability to manipulate lists using fundamental operations. Below are the core techniques you need to know.
You can retrieve a specific element from a list using its index. This isolates the element for further use.
grocery_list = ["milk", "eggs", "cheese"]
print(grocery_list[0])
Output: milk
This operation lets you replace an existing element in a list by assigning a new value to it. It doesn't add new elements, only modifies existing ones.
grocery_list = ["milk", "eggs", "cheese"]
change = "soap"
grocery_list[2] = change
print(grocery_list)
Output: ["milk", "eggs", "soap"]
You can directly update an element by assigning a new value to its index.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list[2] = "fish"
print(grocery_list)
Output: ["milk", "eggs", "fish"]
You can assign the value of one list element to another index within the same list.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list[0] = grocery_list[2]
print(grocery_list)
Output: ["cheese", "eggs", "cheese"]
The insert method adds an element at a chosen index, shifting all subsequent elements down. This increases the list’s length.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.insert(2, "butter")
print(grocery_list)
Output: ["milk", "eggs", "butter", "cheese"]
The append method adds an element to the end of the list, extending its length.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.append("flour")
print(grocery_list)
Output: ["milk", "eggs", "cheese", "flour"]
In Python, the remove method deletes the first occurrence of a specified value from the list.
grocery_list = ["milk", "eggs", "cheese"]
grocery_list.remove("eggs")
print(grocery_list)
Output: ["milk", "cheese"]
The len function returns the number of elements in a list.
grocery_list = ["milk", "eggs", "cheese"]
print(len(grocery_list))
Output: 3
Loops allow you to process each element in a list, either fully or partially. Common algorithms with lists find maximum, minimum, or average values. Linear search (sequential search) checks each element in order until a value is found or all elements are checked.
A complete traversal visits every element in the list. You can use a for loop or a while loop.
For Loop Example
grocery_list = ["milk", "eggs", "cheese"]
for element in grocery_list:
print(element)
Output:
While Loop Example
grocery_list = ["milk", "eggs", "cheese"]
i = 0
while i < len(grocery_list):
print(grocery_list[i])
i += 1
Output:
A partial traversal visits a subset of the list, defined by a start and end index.
For Loop Example
grocery_list = ["milk", "eggs", "cheese", "apples"]start_index = 1
end_index = 3
for i in range(start_index, end_index + 1):
print(grocery_list[i])
Output:
While Loop Example
grocery_list = ["milk", "eggs", "cheese", "apples"]
i = 1
while i < 4:
print(grocery_list[i])
i += 1
Output:
1. What are list operations in Python? | ![]() |
2. How can I access elements in a list by index? | ![]() |
3. What does it mean to directly assign a value to a list element? | ![]() |
4. How do I append an element to the end of a list? | ![]() |
5. What is the method to find the length of a list in Python? | ![]() |