Skip to main content
Albero Academy

Tuples

Ordered, immutable sequences — safe fixed collections and multiple return values.

Data Structures9 min

A tuple is an ordered collection like a list, but immutable — once created it can't change. That constraint is a feature: it signals "this grouping is fixed", makes the value hashable (usable as a dict key), and prevents accidental modification.

Creating & reading tuples

tuples
point = (3, 4)
point[0] # 3 index like a list
len(point) # 2
# point[0] = 9 # ERROR — tuples can't be changed
single = (5,) # a one-item tuple needs the trailing comma
empty = ()

Unpacking & multiple return

Tuples power two everyday idioms: assigning several names at once, and returning multiple values from a function:

unpacking in action
x, y = (3, 4) # x=3, y=4
a, b = b, a # swap — the right side is a tuple
def min_max(xs):
return min(xs), max(xs) # returns a tuple
lo, hi = min_max([5, 2, 9]) # unpack it → lo=2, hi=9
first, *rest = [1, 2, 3, 4] # first=1, rest=[2,3,4]

Why immutability matters

Key takeaways

  1. 01Tuples are ordered and immutable — they cannot change after creation.
  2. 02A one-item tuple needs a trailing comma: (5,).
  3. 03Tuple unpacking assigns several names at once; functions return multiple values as tuples.
  4. 04Being immutable/hashable, tuples can be dict keys and set members — lists cannot.
  5. 05Use tuples for fixed records; use lists for collections that change.

Finished this chapter?

Mark it complete to track your progress through Python.