π

All Subjects

Β >Β

β¨οΈΒ

AP Comp Sci P

Β >Β

π±

Big Idea 3: Algorithms and Programming

# Lists

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:
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:
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...
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.
Like this!
```grocery_list = ["milk", "eggs", "cheese"] grocery_list[0] = grocery_list[2] print (grocery_list)```
The code's output: ["cheese", "eggs", "cheese"]

Inserting elements at a given index
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
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
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
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.
Common algorithms used with lists will often find the maximum or minimum value inside the list or the average.

Thousands of students are studying with us for the AP Computer Science Principles exam.
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