18  Lists

Lists are extremely useful in Python, allowing us to store multiple items of ordered data to which we can refer later.

We can create an empty list if we want - to do this we just use two square brackets with nothing in between.

However, you’ll often want to create a list with some elements already in it.

We can also add an extra item to a list.

Note that we didn’t assign the new list back to the variable my_list.

i.e. we didn’t write

my_list = my_list.append(2)

This is because append updates the object without needing to be assigned!

Why is append written after my_list instead of before?

This is because it’s a method of the list class. The dot here is known as dot notation. We’ll cover this more in Object Oriented Programming, but basically a dot is a way of getting to the methods (functions) and attributes (variables) stored in an object (a list object here).

So this line says “Use the append method of a list object

18.1 Indices

Lists are ordered, which means that we can refer to elements in a list by their location - their index.

To refer to an element in a list by its index, we use the following notation :

my_list[x]

where x is the index number of the element we want to reference.

HOWEVER, Python (like many languages) starts counting from 0. Which means the first element of a list has an index of 0, the second an index of 1 etc. This will catch you out if you’re new to coding, especially as a few constructs in Python start counting from 1…

So, to reference the fourth element in a list my_list, we would use :

my_list[3]

Let’s run this below! Try changing the index (the ‘3’) in the line print(my_list[3])

18.2 Negative Indexing and Slicing

We can also use negative indexing to refer to an item based on its position from the end of the list. Here, -1 would refer to the last element, -2 to the penultimate element etc.

If we want to refer to multiple items in a list, we can use a slice. Here, we use a colon to denote the indices of the start and end elements that we want.

Tip

The start element is included.

The end element is not included.

18.3 Length and Removal

To find the length of a list (the number of elements it contains), we simply use the len() function :

We can remove an item from a list by specifying the element we want to remove, and using remove :

Or by giving the index of the element we want to remove, and using pop:

If we want to remove the last element from a list, we can just use pop without specifying an index :

18.4 Checking Existence

We can check whether or not an element exists in a list using conditional logic combined with the in keyword :

Or whether something is not in a list by additionally using the not keyword :

18.5 Copying lists

If we want to copy a list, we might be tempted to write this :

my_list = [3, 4, 7, "hello", 2]
copy_of_my_list = my_list

The previous example will create a second reference to the same list. In other words, there is still one list, just two different names for it.

To create a copy, we need to use the copy() function of a list object :

my_list = [3, 4, 7, "hello", 2]
copy_of_my_list = my_list.copy()

Now we have two lists, and can work with them independently (ie changes made to one will not affect the other).