LISTS

First, I would like to start off with a BIG THANK YOU to Mr. Liconti, a teacher at Our Lady of Mount Carmel Secondary School in the Dufferin-Peel Catholic District School Board, for allowing me to use the material from his website for this lesson. He’s a standup guy.

The term array in computer science means a collection of ‘like datatypes’ which are indexed. Simply put, most other languages will allow you to make a table of similar datatypes, meaning you can make a table or strings, integers, floats, etc. For instance, imagine you were making a spreadsheet for your course averages. Your table might look something like this.

ICS3U TGJ3M TTJ3C TCJ3C
77 80 77 82

Notice that the datatypes in this situation are all integers. Python doesn’t care about the ‘like datatypes’ bit. It’s a rebel language that way. It also does not store information based on table headers. We’ll save that for later though.

Lists Syntax

In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.).

Let’s create a sample list that does not use any headers at this point. Suppose we wanted to make a list of those averages mentioned in the intro on this page.

77 80 77 82

Our code would look like the following:

[python]
courseAverages = [77,80,77,82]
[/python]

Notice how we can now store multipe values in one variable? How cools is that?! Let’s look at the sytax of a list.

[python]
# empty list
my_list = []

# list of integers
my_list = [1, 2, 3]

# list of strings
my_list = [‘a’, ‘b’, ‘c’, ‘d’]

# list with mixed datatypes
my_list = [1, "Hello", 3.4]
[/python]

Each variable will now have multiple storage locations. To show this visually, here is what the lists will look like.

my_list (empty list)

[this will be an empty list with no information]

my_list (list of integers)

1 2 3

my_list (list of strings)

a b c d

my_list (mixed integers)

1 Hello 3.4

Sample Code

In this sample, we will create and print a list. Copy the code below and execute it. Does it create the result you expected? Notice that it prints the entire lists, including the brackets, and not each individual element. More on that later.

[python]
elementList = [1, "banana", 3.14]
print("The list elements are:", elementList)
[/python]

List Elements

So far we know how to create a list and print it to the screen. How does one go about accessing just one element in that list? For instance, what if I only wanted to see my ICS3U mark and not the whole list?

You can access a list elements by using its index. A list index is an indentifier that tells Python where the item is located within the list. Since Python is a zero-indexing language (think of how range() starts at zero instead of 1), that index within a list will start at zero as well. Let’s take a look at the marks from the previous example.

77 80 77 82

Instead of giving each item a header, Python will give it an index. This means the first item will have an index of 0, the next one will be 1, then 2, and so on. If we were to create a table for it, it would look something like the following:

[0] [1] [2] [3]
77 80 77 82

Since each element has an index, we can now access them individually instead of having to print the whole list.

Sample Code

In this sample, we will create a list and print each of it’s individual elements. Copy the code below and execute it. Does it create the result you expected? Change the items in the list to see the different results. Try creating your own list and printing each of its items.

[python]
#Create the list
elementList = [1, "banana", 3.14]

"""
The line of code above could also be represented as
elementList[0] = 1
elementList[1] = ‘banana’
elementList[2] = 3.14
"""

#Print the first item (located at index 0)
print("The first item is:",elementList[0])

#Print the second item (located at index 1)
print("The second item is:",elementList[1])

#Print the third item (located at index 2)
print("The third item is:",elementList[2])
[/python]

Negative Indexing

In addition to positive index numbers, we can also access items from the list with a negative index number, by counting backwards from the end of the list, starting at -1. This is especially useful if we have a long list and we want to pinpoint an item towards the end of a list.

Using the list elementList above, the items and indexes can be represtented as the following:

List Name elementList
Items 1 ‘banana’ 3.14
Positive Index 0 1 2
Negative Index -3 -2 -1

Sample Code

In this sample, we will create a list and print each of it’s individual elements using negative indexing. Copy the code below and execute it. Does it create the result you expected? Change the items in the list to see the different results. Try creating your own list and printing each of its items.

[python]
#Create the list
elementList = [1, "banana", 3.14]

"""
The line of code above could also be represented as
elementList[0] = 1
elementList[1] = ‘banana’
elementList[2] = 3.14
"""

#Print the last item (located at index -1)
print("The last item is:",elementList[-1])

#Print the second last item (located at index -2)
print("The second last is:",elementList[-2])

#Print the first item (located at index -3)
print("The first item is:",elementList[-3])
[/python]

Check Your Understanding

Before moving to the next section, let’s see how well you know the current material.

  1. Create a program that will store 5 grocery items into a list called groceries. Print the list as a whole as well as each individual item.
  2. Create a program that will store 6 names into a list. Your program should then print the names in forward and reverse order. For instance, the list Fatima, John, Sandra, Mohamed, Teresa, and Shawn should print:
    • Fatima, John, Sandra, Mohamed, Teresa, Shawn
    • Shawn, Teresa, Mohamed, Sandra, John, Fatima