๐Ÿ“š

All Subjects

ย >ย 

โŒจ๏ธย 

AP Comp Sci P

ย >ย 

๐Ÿ“ฑ

Big Idea 3: Algorithms and Programming

Lists

2 min readโ€ขnovember 16, 2020

minnachow

Minna Chow


AP Computer Science Principlesย โŒจ๏ธ

Bookmarkedย 2.3kย โ€ขย 69ย 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 a 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"]
You can also assign 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"]
and assign 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.

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
๐Ÿ” Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
๐Ÿ•นBig Idea 1: Creative Development
โš™๏ธBig Idea 2: Data
๐Ÿ“ฑBig Idea 3: Algorithms and Programming
๐Ÿ–ฅBig Idea 4: Computer Systems and Networks
โŒจ๏ธBig Idea 5: Impact of Computing
โœ๏ธBlogs
๐Ÿ“Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
๐Ÿ’ช๐Ÿฝ Are you ready for the AP CSP exam?
Take this quiz for a progress check on what youโ€™ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
๐Ÿ“ฑ Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
ยฉ 2021 Fiveable, Inc.