Tuples
Immutable sequences, unpacking, and named tuples
A tuple is an ordered, immutable sequence. Tuples are typically used for fixed-size, heterogeneous records — "a point is an (x, y)" or "a database row is (id, name, email)" — where a list would be the wrong semantic choice. Tuples also serve as dictionary keys, multiple return values, and anywhere you need an immutable, hashable sequence.
Every snippet on this page runs in your browser — no setup required.
Etymology: why 'tuple'?
The name comes from mathematics: single, double, triple, quadruple, quintuple, ... n-tuple. In math, an n-tuple is an ordered list of n elements. Python's tuple is the programming language version of that concept. The trailing letters are often dropped (duple, triple, ...), leaving just "tuple."
Creating tuples
The simplest tuple is written with parentheses, separating items with commas.
Parentheses are often optional when the context is unambiguous.
The trailing comma is what makes a one-element tuple, not the parentheses.
One-element tuple syntax
(1,) is a tuple. (1) is just the integer 1 in parentheses. The trailing comma is mandatory for single-element tuples. This catches newcomers off guard, but the rule is simple: the comma, not the parens, makes the tuple.
The tuple() constructor converts any iterable into a tuple.
Indexing, slicing, iteration
Tuples support the same [i] and [start:stop:step] syntax as lists.
Tuples are immutable: you cannot change them after creation.
Iteration works just like lists.
Why immutability is useful
Two big wins:
- Hashability. Tuples of hashable items are hashable, which means you can use them as dict keys or set members. Lists cannot.
- Safety. A function that returns a tuple is signaling that the shape will not change underneath you. Immutability prevents accidental modification and makes code easier to reason about.
Immutability is not deep
A tuple is immutable, but that does not mean its contents are. If a tuple contains a mutable object like a list, that list can still be modified. Immutability only applies to the tuple's structure (you cannot change which objects it points to), not to the objects themselves.
Real-world uses
Tuples appear constantly in real code:
- Coordinates and points:
(x, y),(lat, lon, alt) - Database rows:
(id, name, email, created_at) - Multiple return values:
return (status, data, error) - Dictionary keys:
cache[(user_id, timestamp)] = result - Color values:
rgb = (255, 128, 0) - Function argument records:
args = (10, "hello", True); func(*args)
Anytime you have a small, fixed-size record, a tuple is often clearer than a list.
Tuple unpacking
This is one of Python's most elegant features. The number of names on the left must match the length of the tuple on the right.
Unpacking works with any iterable, not just tuples.
Star (*) catches the leftover items as a list.
You can use *_ to discard the middle items.
Returning multiple values
Functions "return multiple values" by returning a tuple, which the caller can unpack.
Named tuples
collections.namedtuple and typing.NamedTuple give you a tuple with named fields, perfect for lightweight record types. Named tuples are immutable, just like regular tuples, but you can access fields by name instead of only by index.
The newer typing.NamedTuple version lets you add type hints.
Named tuples are immutable. For mutable record types with methods, reach for @dataclass, which is covered in the Classes page.
Challenges
Define a function split_minmax(values) that returns a tuple (minimum, maximum) of values. Assume values always has at least one item.
A function stats_of(record) is given a 4-tuple (name, scores, attempts, best) where scores is a list. Return a new tuple (name, average_score) where average_score is the mean of scores rounded to one decimal place. Use tuple unpacking to extract name and scores from the record.
Which of the following creates a valid one-element tuple?
(1)
(1,)
[1]
{1}
Why would you choose a tuple over a list for (latitude, longitude)?
Tuples are faster to iterate than lists.
Tuples are immutable and hashable, so they can be used as dict keys.
Lists cannot contain floats.
Tuples support more methods than lists.
What does this code print?
first, *rest = (1, 2, 3, 4)
print(rest)
(2, 3, 4)
[2, 3, 4]
2
A ValueError
Next: dictionaries, Python's most-used data structure.