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
IndexErrorand 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.
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).
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.
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.
Now try to change a tuple. Press Run:
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.
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.
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.
- The Python Tutorial: An Informal Introduction to Python, Lists
- The Python Tutorial: Data Structures, More on Lists: append and other list methods
- The Python Tutorial: Tuples and Sequences: tuples, and unpacking them
- Built-in Types: Lists
- Built-in Types: Tuples
- Built-in Types: Common Sequence Operations: indexing, len and in, for lists, tuples and text
- Built-in Exceptions: IndexError