Dataslope logoDataslope

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.

Code Block
Python 3.13.2
Code Block
Python 3.13.2

Parentheses are often optional when the context is unambiguous.

Code Block
Python 3.13.2

The trailing comma is what makes a one-element tuple, not the parentheses.

Code Block
Python 3.13.2

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.

Code Block
Python 3.13.2

Indexing, slicing, iteration

Tuples support the same [i] and [start:stop:step] syntax as lists.

Code Block
Python 3.13.2
Code Block
Python 3.13.2

Tuples are immutable: you cannot change them after creation.

Code Block
Python 3.13.2

Iteration works just like lists.

Code Block
Python 3.13.2

Why immutability is useful

Two big wins:

  1. Hashability. Tuples of hashable items are hashable, which means you can use them as dict keys or set members. Lists cannot.
  2. 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.
Code Block
Python 3.13.2

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.

Code Block
Python 3.13.2

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.

Code Block
Python 3.13.2

Unpacking works with any iterable, not just tuples.

Code Block
Python 3.13.2

Star (*) catches the leftover items as a list.

Code Block
Python 3.13.2
Code Block
Python 3.13.2

You can use *_ to discard the middle items.

Code Block
Python 3.13.2

Returning multiple values

Functions "return multiple values" by returning a tuple, which the caller can unpack.

Code Block
Python 3.13.2
Code Block
Python 3.13.2

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.

Code Block
Python 3.13.2

The newer typing.NamedTuple version lets you add type hints.

Code Block
Python 3.13.2

Named tuples are immutable. For mutable record types with methods, reach for @dataclass, which is covered in the Classes page.

Challenges

Challenge
Python 3.13.2
Split min and max

Define a function split_minmax(values) that returns a tuple (minimum, maximum) of values. Assume values always has at least one item.

Challenge
Python 3.13.2
Unpack a record

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.

QuestionSelect one

Which of the following creates a valid one-element tuple?

(1)

(1,)

[1]

{1}

QuestionSelect one

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.

QuestionSelect one

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.

On this page