Phase 2 · Data StructuresModule 9~28 min read

Tuples & Sequences

Immutable sequences, elegant unpacking, and named tuples.

What you'll learn

A tuple is like a list, but immutable — once created, it can never change. That constraint is a feature: tuples are perfect for fixed collections of values, and they power one of Python's most elegant tricks, unpacking.

By the end you'll be able to:

  • Create tuples and understand their immutability
  • Unpack tuples (and swap variables) elegantly
  • Give tuple fields names with namedtuple
  • Use the operations shared across all sequences
  • Choose a tuple vs a list for the job

Creating tuples

A tuple is written with parentheses (though the commas are what really make it). Everything you can do to read a list — indexing, slicing, len(), in — works on tuples too. What you can't do is change one:

tuples.py
point = (3, 4)        # a tuple
single = (42,)        # a one-item tuple NEEDS a trailing comma
empty = ()

print(point[0])       # 3
print(len(point))     # 2
# point[0] = 5        # TypeError — tuples are immutable

The one-element trap

A single-element tuple needs a trailing comma: (42,). Without it, (42) is just the number 42 in parentheses — a very common surprise.

Unpacking

This is where tuples shine. You can unpack a tuple's values straight into separate variables in one line. It's the reason you can return several values from a function and swap two variables without a temporary:

Unpacking a tuple
(3, 4)──▶x, y = point──▶x = 3, y = 4
unpacking.py
point = (3, 4)
x, y = point          # unpack into two names
print(x, y)           # 3 4

# swap variables with no temp — thanks to tuple unpacking!
a, b = 1, 2
a, b = b, a
print(a, b)           # 2 1

# starred unpacking grabs "the rest"
first, *rest = [1, 2, 3, 4]
print(first, rest)    # 1 [2, 3, 4]

You already met this returning multiple values from a function — that's tuple packing and unpacking:

returns.py
def divide(a, b):
    return a // b, a % b   # returns a tuple: (quotient, remainder)

quotient, remainder = divide(17, 5)
print(quotient, remainder)   # 3 2

Named tuples

Accessing tuple fields by index (point[0]) can be cryptic. A namedtuple gives each position a name, so you get readable, self-documenting records — with all the benefits of a tuple:

named.py
from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)

print(p.x, p.y)   # 3 4  — access by NAME, not just index
print(p)          # Point(x=3, y=4)

Note

A namedtuple is a lightweight, immutable record. For richer data classes with defaults and methods, you'll meet @dataclass in Module 17 — but namedtuples are perfect when you just need a tidy, named tuple.

Shared sequence operations

Lists, tuples, and strings are all sequences, so they share a common set of operations — which is why the same skills transfer everywhere:

  • Indexing and slicing: seq[0], seq[1:3], seq[::-1]
  • Length and membership: len(seq), x in seq
  • Concatenation and repetition: seq + seq2, seq * 3
  • Iteration: for item in seq

Tuple vs list — when to use which

The choice comes down to one question: does this collection need to change?

Immutable vs mutable

Tuple — immutable ( )

  • • Cannot change after creation
  • • Slightly faster & memory-light
  • • Can be a dictionary key
  • • Great for fixed records (a coordinate)

List — mutable [ ]

  • • Add, remove, and change items
  • • Grows and shrinks
  • • Cannot be a dictionary key
  • • Great for a changing collection

Tip

A good default: use a tuple for a fixed group of related values (a point, an RGB colour, a database row) and a list for a collection that grows or changes. Tuples also signal intent — "this shouldn't change" — to anyone reading your code.

Recap & quick check

Key takeaways

  • A tuple is an immutable sequence, written with parentheses (and commas).
  • A one-element tuple needs a trailing comma: (42,).
  • Unpacking assigns a tuple's values to separate variables — enabling multi-return and swapping.
  • namedtuple gives fields names for readable, immutable records.
  • Lists, tuples, and strings share sequence operations; use a tuple when data shouldn't change.

Quick check

1. What is the key difference between a tuple and a list?

2. How do you write a one-element tuple?

3. What does 'a, b = b, a' do?

4. What does a namedtuple add over a regular tuple?

5. Can a tuple be used as a dictionary key?

Nicely done. Next up: Module 10 — Dictionaries, the most important data structure in Python.