What are Lists?

Lists are collections of data. Lists are incredibly helpful because you can use them to store unlimited amounts of data, and using loops and functions that locate list data using indexes, the information can be procedurally used in a way that suits a certain purpose.

Here is the list syntax in Python:

list = ["item0", "item1", "item2"]
print(list)
['item0', 'item1', 'item2']

Indexes

Most code languages count starting with zero, and this is also the case for list indexes. See the code below for demonstration.

indexlist = [1, 2, 3, 4]
x = indexlist[0] #the syntax for calling list indexes
#even though it's technically the 1st in the list, indexes start at 0
print("List index '0' is " + str(x) + ".")
#the list has four items, so the highest index is 3
print("List index '3' is " + str(indexlist[3]) + ".") #no variable necessary

#trying to call a list index that's out of the range causes an error
try: #using try/except statement to demonstrate
    print(indexlist[4])
except: #except condition occurs when an attempted action causes an error
    print("This statement means that calling 'indexlist[4]' failed.")
List index '0' is 1.
List index '3' is 4.
This statement means that calling 'indexlist[4]' failed.

Numbers in lists can be used for math. This is made especially useful by loops, which we'll go over later.

print(indexlist[0] + indexlist[2]) #translates to 1 + 3
4

Operations on Lists

Here is a table with college board pseudocode, its associated syntax in python, and a brief description of it's function. You can reference this table when you do your homework and should add it to your notes!

Pseudocode Operation Python Syntax Description
aList[i] aList[i] Accesses the element of aList at index i
x ← aList[i] x = aList(i) Assigns the element of aList at index i
to a variable 'x'
aList[i] ← x aList(i) = x Assigns the value of a variable 'x' to
the element of a List at index i
aList[i] ← aList[j] aList[i] = aList[j] Assigns value of aList[j] to aList[i]
INSERT(aList, i, value) aList.insert(i, value) value is placed at index i in aList. Any
element at an index greater than i will shift
one position to the right.
APPEND(aList, value) aList.append(value) value is added as an element to the end of aList
and length of aList is increased by 1
REMOVE(aList, i) aList.pop(i)
OR
aList.remove(value)
Removes item at index i and any values at
indices greater than i shift to the left.
Length of aList decreased by 1.

Challenges

We are going to walk through these operations one by one with the following examples. Make sure you follow along and fill these out as we go. run python

grocery_list = ['apples', 'milk', 'oranges', 'carrots', 'cucumbers']

# Print the fourth item in the list


# Now, assign the fourth item in the list to a variable, x and then print the variable


# Add these two items at the end of the list : umbrellas and artichokes



# Insert the item eggs as the third item of the list 


# Remove milk from the list 


# Assign the element at the end of the list to index 2. Print index 2 to check


# Print the entire list, does it match ours ? 


# Expected output
# carrots
# carrots
# artichokes
# ['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']