πŸ“š

Β >Β 

⌨️ 

Β >Β 

πŸ“±

3.10 Lists

3 min readβ€’june 18, 2024

Minna Chow

Minna Chow

Milo Chang

Milo Chang


AP Computer Science Principles ⌨️

80Β resources
See Units

Basic List Operators

On the AP exam, you'll be asked to evaluate some basic operations on lists.
Remember, the AP Pseudocode's index starts at 1.

Accessing an element by index

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-KJoWquhx0DGw.png?alt=media&token=19b940a1-2b60-44c2-9fd2-07cfb1b3ae58
This operation allows you to single out an element in a list based on its index number. You can then interact with only this element.
grocery_list = ["milk", "eggs", "cheese"] print (grocery_list[0])
The code's output:
milk

Assigning the value of an element of a list to a variable

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-baTea39nk3FF.png?alt=media&token=a16b04a1-200a-41bd-a044-91f4619284dd
This allows you to assign a variable to a certain element within a list, changing the element. Note that you wouldn't use this operation to add new values to the list, only to change ones already existing.
grocery_list = ["milk", "eggs", "cheese"] change = "soap" grocery_list[2] = change print (grocery_list)
The code's output: ["milk", "eggs", "soap"]

Assigning a value to an element outright

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-G7K82tuOivL3.png?alt=media&token=5e5ce976-66a5-4657-ba7b-f56206ff0191
Here's an example in Python:
grocery_list = ["milk", "eggs", "cheese"] grocery_list[2] = "fish" print (grocery_list)
The code's output: ["milk", "eggs", "fish"]

Assigning the value of one element in the list to another

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-HIkHRVI5iX2i.png?alt=media&token=5040c704-7717-4ef9-a3c5-e64be93b0644
Like this!
grocery_list = ["milk", "eggs", "cheese"] grocery_list[0] = grocery_list[2] print (grocery_list)
The code's output: ["cheese", "eggs", "cheese"]

Adding and Removing Elements

Inserting elements at a given index

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-A0rzwUOFVssF.png?alt=media&token=34f181f3-0b95-4bae-ad53-70376930fe63
This allows you to insert a value into the index position you want. It will increase the length of the list and shift everything greater than or equal to that index down by one place. For example, if you were to insert a new value at the index value 4, what was originally there will move to the index value 5, 5 will move to 6, and so on.
grocery_list = ["milk", "eggs", "cheese"] grocery_list.insert (2, "butter") print (grocery_list)
The code's output: ["milk", "eggs", "butter", "cheese"]

Adding elements to the end of the list

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-sRuzmgnfwzBM.png?alt=media&token=d1f7026a-6e4c-49db-b9fd-c41b8ff157a8
This allows you to add values to the end of your list.
grocery_list = ["milk", "eggs", "cheese"] grocery_list.append ("flour") print (grocery_list)
The code's output: ["milk", "eggs", "butter", "flour"]

Removing elements

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-o6qNvxCMH8uy.png?alt=media&token=9904c81f-8db5-48dc-a230-bc6a639cf7f6
You can also remove elements.
In Python, you remove items based on element value rather than index number.
grocery_list = ["milk", "eggs", "cheese"] grocery_list.remove ("eggs") print (grocery_list)
The code's output: ["milk", "cheese"]

Determining the length of a list

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-MhIhEgrLwQJy.png?alt=media&token=cd830cee-a81b-4ed8-bad2-0f8cbeac3824
This will tell you what the length of your list is.
grocery_list = ["milk", "eggs", "cheese"] print (len (grocery_list))
The code's output: 3

Looping through Lists

You can also use loops to traverse, or go through, a list. This can either be a complete traversal or a partial traversal, depending on what your loop specifies.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-BJM6keUgNa6g.png?alt=media&token=386f87a3-72d4-472c-91c9-005e098c3269
Common algorithms used with lists will often find the maximum or minimum value inside the list or the average.
Linear search or sequential search algorithms check each element of a list, in order, until the desired value is found or all elements in the list have been checked.

Complete Traversal

In Python, you can perform a complete traversal of a list using either a for loop or a while loop. Both implementations are shown below.
grocery_list = ["milk", "eggs", "cheese"] # for loop implementation for element in grocery_list: print(element)
The code's output:
milk
eggs
cheese
grocery_list = ["milk", "eggs", "cheese"] # while loop implementation i = 0 while i < len(grocery_list): print(grocery_list[i]) i += 1
The code's output:
milk
eggs
cheese

Partial Traversal

In Python, you can also perform a partial traversal of a list using either a for loop or a while loop. Both implementations are shown below.
grocery_list = ["milk", "eggs", "cheese", "apples"] # for loop implementation start_index = 1 end_index = 3 for i in range(start_index, end_index + 1): print(grocery_list[i])
The code's output:
eggs
cheese
apples
grocery_list = ["milk", "eggs", "cheese", "apples"] # while loop implementation i = 1 while i < 4: print(grocery_list[i]) i += 1
The code's output:
eggs
cheese
apples

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.