Basics · lesson 9 of 10
Parts in this lesson
A[a, b, c]a list
B[0] [-1]from 1.5, now for lists
C.append()adds to the end
Dfor x in listeach item in turn
E(a, b, c)a tuple: fixed

Lists and tuples

Last updated

A library has more than one book. In this lesson you'll keep many values together in a list, read them by position, add to the list, loop over everything in it, keep fixed groups of values in tuples, and read the error you get when you reach past the end.

By the end of this lesson you can

  • Make a list, read its items by position, add with .append() and change an item
  • Loop over every item in a list with for
  • Use a tuple for a fixed group of values, and unpack it into names
  • Read an IndexError and fix it

Many values, one name

A list holds several values in order, written between square brackets with commas between them. The values in a list are called its items, and they can be any type.

You read an item by its position, exactly as you read characters from text in lesson 1.5. The first item is [0], and [-1] is the last. len() gives the number of items.

Step 1 code main.py
Edit the code, then run it again

Lists can grow and change

.append() is a list method that adds an item to the end. A list can start empty, [], and grow as the program runs. You can also replace an item by giving its position a new value (line 5).

Step 2 code main.py
Edit the code, then run it again

Here lists differ from text. .strip() gave back new text and left the original alone, but .append() changes the list itself. Lists can change; text can't. Python's word for "can change" is mutable, and in Stage 3 you'll see why it matters.

Each item in turn

In lesson 1.7, for looped over the numbers from range. It can loop over a list just as well: each round, the name points at the next item. No positions needed.

Watch it run. Lines 4 to 6 use the running total from lesson 1.7, this time over a list of page counts.

Step 3 code main.py
Edit the code, then run it again

When you need the items, loop over the list directly, as here. Looping over range(len(titles)) and writing titles[i] works too, but it's longer and gives more room for off-by-one mistakes.

Fixed groups: tuples

A tuple is like a list, but written with round brackets, and it can never change: no .append(), no replacing items. Use a tuple for a small group of values that belong together, like a book's title, author and year, and a list for a collection that grows.

Line 4 shows unpacking: put as many names as the tuple has items on the left of =, and each name gets one item.

Step 4 code main.py
Edit the code, then run it again

Now try to change a tuple. Press Run:

Step 4 code main.py
Edit the code, then run it again

TypeError: 'tuple' object does not support item assignment. That's the tuple doing its job: once made, it stays as it is.

Past the end

This list has three items, and the program asks for the item at position 3. Press Run and read the error.

Step 5 code main.py
Edit the code, then run it again

IndexError: list index out of range. The positions of a 3-item list are 0, 1 and 2, so there's no position 3. Change it to titles[2], or to titles[-1], which always means the last item, however long the list is.

Check what you learned

Four questions. Pick or type an answer, then check it.

What does this program print?
loans = ["Dune"]
loans.append("Emma")
loans.append("Dune")
print(len(loans))
shelf = ["Dune", "Emma", "Beloved", "Matilda"]. Which gives "Matilda"?
When should you use a tuple instead of a list?
title, year = ("Emma", 1815). What is year?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python