Lists in Python

Using single numbers or data points will only get us so far. If we want to analyse data sets or do anything with more than one number, it would be very helpful to have tools that grouped numbers. Fortunately, Python gives us several of these which will help to solve different problems. This article looks at one of these tools – ‘Lists’. We will learn what a list is, how we can use them in functions, and methods that help us to get more use out of them.

Lists are our simplest way to group numbers. Surrounded by [square brackets], they are simply a list of values – whether that is numbers, text, variables or even another list. Let’s put our ‘goals for’ and ‘goals against’ from our team’s first 9 games into a list:

In [1]:
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

print(GF)
print(GA)
[0, 0, 3, 2, 1, 5, 3, 1, 2]
[2, 0, 2, 2, 3, 2, 3, 2, 4]

Accessing numbers in lists is easy. Let’s take the numbers from our first game.

We do this by calling the list name, and adding square brackets with a number on the end. We will get the value held in the positon of the number.

This order starts at 0 – so for the first game, we will put 0. The second, we put 1, the third, we put 2 and so on.

In [2]:
print(GF[0])
print(GA[0])
0
2

Looks like we lost 2-0. Let’s make sure:

In [3]:
GF[0] - GA[0]
Out[3]:
-2

Yup, a loss and -2 goal difference.

Let’s iterate through this list to get our goal difference and also our points for the season so far.

In [4]:
#Define variables for points and goal difference
#Set goals for and against

P = 0
GD = 0
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

#We have 9 games, so we iterate with a range of 9 in a for loop
for i in range(9):
    #If we score more, give us 3 points and add the goal difference
    if GF[i] > GA[i]:
        P += 3
        GD += (GF[i] - GA[i])
    #If we draw, give us a point. Goal difference will stay the same.
    elif GF[i] == GA[i]:
        P += 1
    #If we lose, we get no points, but update the goal difference
    else:
        GD += (GF[i] - GA[i])

print(P)
print(GD)
9
-3

Range

In those 9 games, we received 9 points and had a -3 GD. We can change the lists to be any length, and change the number in the ‘range’ to calculate this over any number of games!

‘Range’ is a function that generates a series of numbers between two points. By default, it starts at 0 and goes to whatever number we give it. Let’s take a look at some examples:

In [5]:
#One argument in range()
#Give me the list of 10 numbers, starting at 0
list(range(10))
Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [6]:
#Two arguments in range()
#Give me all numbers between these two
list(range(5,10))
Out[6]:
[5, 6, 7, 8, 9]
In [7]:
#Three arguments in range()
#Give me all numbers between the first two, in steps of the third number
list(range(0,10,2))
Out[7]:
[0, 2, 4, 6, 8]

We then used this range as our counter for how many times we used the for loop. Keep this in mind for future loops!

Selecting a number from a list

Selecting a number from a list is easy. Just pop the index (the location of the number) in square brackets after the list name.

Select multiple with a colon between two numbers – including the first of them, but not the last.

In [8]:
Players = ["Didek","Bascin","Smacer","Boras","Finnon"]

#Select the 4th player - remember that the count starts at 0!
print(Players[3])

#Select the middle 3 players (Bascin, Smacer and Boras)
print(Players[1:4])
Boras
['Bascin', 'Smacer', 'Boras']

List Methods

A method is a function that belongs to a data type. It is used to manipulate or utilise this data type.

For lists, this might involve adding something to the end, alphaetising our list or finding the location of something within it.

A method is used by calling the name of the variable, then the method and its arguments – you can find a few below:

In [9]:
GF = [0,0,3,2,1,5,3,1,2]
GA = [2,0,2,2,3,2,3,2,4]

#Let's add a new score to our goals for and goals against lists
#The append() method will help us here

GF.append(3)
GA.append(1)

#Show the scores, 3-1 should be the final column
print(GF)
print(GA)
[0, 0, 3, 2, 1, 5, 3, 1, 2, 3]
[2, 0, 2, 2, 3, 2, 3, 2, 4, 1]
In [10]:
GF = [0, 0, 3, 2, 1, 5, 3, 1, 2, 3]
GA = [2, 0, 2, 2, 3, 2, 3, 2, 4, 1]

#I want to reverse the scores, to see the 5 most recent ones
#Reverse() will make this light work

GF.reverse()
GA.reverse()
print(GF[0:5])
print(GA[0:5])
[3, 2, 1, 3, 5]
[1, 4, 2, 3, 2]

There are plenty more methods for you to learn about on your journey learning Python – I’m sure you’ll find lots more on the pages here, but a quick Google will take you to many more.

Summary

Lists are a simple way to store lots of numbers under one variable. This is very useful for keeping records of things, or for running formulas that take more than one number.

We have learned that they are created with square brackets, can be navigated to find individual datapoints or ‘slices’ of then.

We also learned about methods – functions that act on a particular class. For lists, we saw them easily add more data points, or reverse the order of them.

There are more ways to store multiple data points – take a read on ‘tuples’ and ‘dictionaries’ to learn more!

Leave a Reply