Lesson 18 of 30 · Collections
Tuples
Learn Python tuples: ordered collections that cannot be changed. See how to create and unpack them, and when a tuple is a better choice than a list.
- 7 min read
- Beginner
- Reviewed
- What a tuple is and how it differs from a list
- How to unpack a tuple into separate variables
- When a tuple is the right choice
A list that cannot change
A tuple is an ordered collection, like a list, with one big difference: once created, it cannot be changed. You cannot add, remove or replace its items.
Tuples are written with round brackets.
point = (3, 4)
colour = (255, 128, 0)
person = ("Ada", 36, "London")
print(point)
print(person[0])
print(len(colour))Reading works exactly as it does for lists: indexes, negative indexes, slices, len(), in and for loops are all the same.
Tuples are immutable
Something that cannot be changed is called immutable. Try to change a tuple and Python stops you.
point = (3, 4) point[0] = 10
You get TypeError: 'tuple' object does not support item assignment.
Why would you want that?
It sounds like a limitation, but it is a feature.
- Safety. If data should never change, such as the coordinates of a city or the days of the week, a tuple guarantees it will not be changed by accident.
- Meaning. A tuple tells the reader "these values belong together as one thing". A list says "here are several things of the same kind".
days = ("Mon", "Tue", "Wed", "Thu", "Fri") # fixed: a tuple
tasks = ["email", "meeting"] # will grow: a list
tasks.append("lunch")
print(days)
print(tasks)Unpacking
The number of variables must match the number of items.
person = ("Ada", 36, "London")
name, age, city = person
print(name)
print(age)
print(city)Unpacking makes swapping two variables very neat.
a = 1
b = 2
a, b = b, a
print("a =", a)
print("b =", b)Looping over a list of tuples
Unpacking also works directly in a for loop. This is a pattern you will see often.
students = [("Ada", 92), ("Grace", 85), ("Alan", 78)]
for name, score in students:
print(f"{name} scored {score}")A tuple with one item
A single value in brackets is not a tuple. You need a trailing comma.
not_a_tuple = (5) a_tuple = (5,) print(type(not_a_tuple)) print(type(a_tuple))
Converting between lists and tuples
numbers = [1, 2, 3] frozen = tuple(numbers) editable = list(frozen) print(frozen) print(editable)
Lists or tuples?
| List | Tuple | |
|---|---|---|
| Brackets | [1, 2, 3] | (1, 2, 3) |
| Can be changed | Yes | No |
| Use it for | A collection that grows, shrinks or is reordered | A fixed group of related values |
When in doubt, use a list. Choose a tuple when the values must stay fixed.
Common mistakes
Unpack point into x and y, then print x=3, y=4.
point = (3, 4) # Unpack the tuple into two variables called x and y, # then print exactly: x=3, y=4
What happens when you try to change an item in a tuple?
Summary
- A tuple is an ordered collection that cannot be changed.
- Tuples use round brackets:
(3, 4). - Unpacking assigns each item to its own variable:
x, y = point. - Use a list for data that changes and a tuple for a fixed group of values.
Next: a collection where every item is unique.
Finished reading? Tick it off to track your progress.