Drop links or images here to add them to the editor.

Every piece of data your programs have stored so far has had a variable of its own. That works right up to the moment there is more than a handful of it. Storing the names of three players in a game takes three variables:

player1 = "Mary"
player2 = "Sean"
player3 = "Atif"

Three is manageable. Thirty is not, and you cannot write a program that copes with a number of players you do not know in advance, because every variable has to be typed out before the program runs.

A list solves that. A variable stores one piece of data under one identifier. A list stores as many pieces of data as you like under one identifier:

players = ["Mary", "Sean", "Atif"]

That is one name, players, holding three items. A fourth player does not need a fourth variable.

A list is an example of a data structure: a way of organising several pieces of data so that a program can work with them together.

You may hear lists called arrays in other programming languages. There are small differences between the two, but at this stage you can treat them as the same idea.

Creating a list

A list is created with an assignment, exactly like a variable, but the data on the right is written between square brackets:

players = ["Mary", "Sean", "Atif"]

There are three parts to it.

A list is worth naming in the plural, because it holds several things at once. players reads better than player for a list of three names.

A list can hold text or numbers

Each item in a list of text needs its speech marks, just as a piece of text does anywhere else. A list of whole numbers does not:

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

prime_numbers = [1, 3, 5, 7, 11, 13]

Every item has an index

To pick out one item, Python numbers the positions in the list. That number is called the index, and the numbering starts at zero:

Index 0 1 2
Item "Mary" "Sean" "Atif"

"Mary" is the first item in the list, but its index is 0, not 1. This is the single easiest thing to get wrong about lists, because everywhere else in life counting starts at one.

It also means the last index is always one less than the number of items. A list of three items has indices 0, 1 and 2. A list of five items has indices 0 to 4.

Outputting one item

Outputting one item from a list uses print, the same as outputting anything else. Inside the normal brackets goes the name of the list, and after it, in square brackets, the index of the item you want:

players = ["Mary", "Sean", "Atif"]

print(players[2])

That prints Atif, because Atif is the item at index 2.

Both kinds of bracket are doing a job here, so neither can be left out:

Outputting the whole list

Printing an entire list takes one line:

players = ["Mary", "Sean", "Atif"]

print(players)

Python prints the list the way it is written in code, square brackets and all:

['Mary', 'Sean', 'Atif']

Look carefully at that output, because it is not the same as printing the three names one at a time. The square brackets are there, every piece of text is in quotes, and there is a comma and a space between the items. That is exactly what Python prints whenever a whole list is printed, so it is worth recognising.

Letting the user choose which item to output

The index in the square brackets does not have to be typed out in full. It can be a variable, which means the user can choose:

countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

print("Enter a number between 0 and 4:")
position = int(input())
print(countries[position])

An index has to be a whole number, so the answer needs int() around it, just as it does before any other calculation. Without the cast, input hands back text, and Python stops with:

TypeError: list indices must be integers or slices, not str

Asking for an item that is not there

countries has five items, so its indices run from 0 to 4. Asking for index 5 is asking for a sixth item that does not exist, and Python says so:

IndexError: list index out of range

It is not a mistake Python quietly ignores: the program stops on that line. Whenever an index comes from the user, it is worth thinking about what happens if the number they type is too big.

Try it yourself

Run the code below and check each line of output against the list.

players = ["Mary", "Sean", "Atif", "Steve", "Lucy"]

print(players)
print(players[3])
print(players[1])
print(players[3] + players[4])

The last print joins two items together with +, exactly as it would join two pieces of text held in variables, so the two names come out with no space between them.

Now add one more line at the bottom, print(players[5]), and run it again. Work out first why that line cannot work, then check the error message against what you predicted.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The list name is spelled identically everywhere it is used, capital letters included.
  • The index is surrounded by square brackets.
  • List indexing starts at zero. Are you counting properly?
  • The largest index in a list is one less than the number of items in it.
  • An index that comes from input is wrapped in int().