Official Documentation

So far in our programs we’ve tackled different issues of scale:

  • For repeating the same instructions many times, we can use loops
  • For reusing small modules of code in different contexts, we can use functions

So we’ve handled scale as it relates to instructions… but what about data? If we wanted to write a program that handles a LOT of data, we’d need a LOT of variables to do so! Is there a Python mechanism that allows us to handle large quantities of data, without needing us to create TONS of variables? Enter… the List data structure!

What is a List?

A list is like a variable that has lots of values! They’re used to store HUGE amounts of data that we can easily reference. For example, we could create a new list like so:

myList = [6, 92, 3, -50, 400]

The list above has five ELEMENTS, meaning it has five different values, all tucked in their own little slots. We’ll discuss soon how you can access each of the numbers in the list above. But first, let’s talk about some other list properties.

Types

Lists in Python can hold multiple types:

myList = ["Thing", 87, True]

The list above holds three different data types: a string, an int, and a boolean.

Length

To know how long a list is, you can use the len function:

myList = [8.5, 3.56, 2.222, 47.89]
x = len(myList)

In the code above the value of x would be 4, since there are four elements in the list.

Adding/Removing elements from the List

You can add a new element to the list using the following syntax:

myList = [5, 63, 4100]
 
myList.append(77)

The list would now look like:

5, 63, 4100, 77

Similarly, you can remove elements with the following syntax:

#Given that our list is [5, 63, 4100, 77]
myList.remove(63)

This removes the first occurrence of 63 in the list. Thus, the list would now look like:

5, 4100, 77

Accessing the List 1: Square Brackets Notation

So now that we’ve established that Lists can hold multiple pieces of data, how do they actually address that problem of scale we were talking about earlier? The way you access the elements of the list should provide some insight!

There are a couple different ways we can access the elements in a list. The first is by using square bracket notation, like

myList = ["Thing", 87, True]
 
x = myList[0]

The variable x now has a value of “Thing”. That’s because we are accessing the first element in our list, which starts at index 0.

We could extend this further:

myList = ["Thing", 87, True]
 
x = myList[0]
y = myList[1]
z = myList[2]

Just as before, x will now have a value of "Thing". Additionally, y will have a value of 87 and z will have a value of True. We have used the square brackets notation to access all of the different elements in our list!

Here we finally address our problems of scale! Consider that we might want to loop through all of the elements of our list and print them out. We could do so with the syntax below:

myNums = [77, 2004, -988, 5256, 30]
 
for i in range(len(myNums)):
	print(myNums[i])

To walk through this step by step:

  1. We have our list, full of numbers!
  2. We set up a for loop with a range
  3. Using the len function on the list is what gets us the number 5, which we put in our range
  4. Since i ranges from 0 to 4, we will print out all the elements in the list

This is amazing! Instead of putting the individual numbers into the square brackets, we can use a variable instead!

So the value of i, as the loop is being run, will be:

0
1
2
3
4

But the value of myNums[i] will be:

77
2004
-988
5256
30

Accessing the List 2: Directly

There is another way to loop through our elements. Instead of using the square brackets notation, we can access them directly in our for loop like so:

myNums = [77, 2004, -988, 5256, 30]
 
for x in myNums:
	print(x)

This looks way different than the square brackets stuff, and way easier too! What’s going on here? Instead of creating a new variable called i and treating it like an index number (by using the range function), we’re instead creating a variable called x and using it to store the individual list elements as we walk through our loop! So the value of x, as the loop is being run, will be:

77
2004
-988
5256
30

A note of caution: you can’t combine these two different syntaxes:

myNums = [77, 2004, -988, 5256, 30]
 
for x in myNums:
	print(myNums[x])

This would be disastrous! Instead of running through our individual elements by index the correct way:

myNums[0]
myNums[1]
myNums[2]
myNums[3]
myNums[4]

you’d be doing this:

myNums[77]
myNums[2004]
myNums[-988]
myNums[5256]
myNums[30]

List Errors

Lists are tricky because they introduce a lot of new trouble spots to our code. For example, what if we were doing the following:

myNums = [77, 2004, -988, 5256, 30]
 
for i in range(6):
	print(myNums[i])

This code would access the following elements:

myNums[0]
myNums[1]
myNums[2]
myNums[3]
myNums[4]
myNums[5] #???

Whose values are:

77
2004
-988
5256
30
????????

Whoops! We tried to access myNums[5], which is one element too far. We’re accessing an element that… doesn’t exist in our list! You will see an error that looks something like this:

IndexError: list index out of range

This means you somehow are trying to go further in your list than is possible. So look at your code again, and figure out how that’s happening! In our case above, we should use the length of the list rather than some arbitrary number.