Menu

Python Tuples: Immutable Sequences, Unpacking, and Named Tuples

When to reach for a tuple instead of a list — immutability, unpacking, named tuples, and the scenarios tuples are made for.

A Tuple Is a List You Can't Change

Tuples and lists look similar at a glance — both are ordered sequences, both support indexing and iteration — but one critical difference changes everything: tuples are immutable. Once you create a tuple, you can't add to it, remove from it, or swap out an item.

You write tuples with parentheses (or sometimes without):

main.py
Output
Click Run to see the output here.

A tuple with one item needs a trailing comma, or Python treats the parentheses as grouping:

main.py
Output
Click Run to see the output here.

And for empty tuples, use ():

main.py
Output
Click Run to see the output here.

Immutability, in Practice

Try to change a tuple and Python stops you:

main.py
Output
Click Run to see the output here.

You can read from a tuple freely — indexing, slicing, iterating, in — you just can't modify it. If you need a different tuple, you create a new one.

Why Would You Want Something You Can't Change?

Immutability sounds like a limitation, but it's often the feature you want.

  1. Safety. A tuple you pass into a function can't be mutated behind your back.
  2. Hashability. Tuples of immutable values can be used as dictionary keys or set members; lists can't.
  3. Intent. A tuple says "this is a fixed-shape record." point = (3, 4) communicates "two coordinates" more clearly than point = [3, 4] does.

Tuples shine for things like:

  • Coordinates: (x, y), (lat, lon)
  • RGB colors: (255, 128, 0)
  • Records: (name, email, signup_date)
  • Returning multiple values from a function

Unpacking

Tuples and the unpacking syntax are made for each other:

main.py
Output
Click Run to see the output here.

The comma on the left is what does the unpacking. Python sees two names and a tuple with two values, and it binds them pairwise.

This is everywhere in idiomatic Python:

main.py
Output
Click Run to see the output here.

You can use * to collect "the rest":

main.py
Output
Click Run to see the output here.

Handy when you only care about the first and last elements.

Tuples as Dictionary Keys

Because tuples are hashable (as long as their contents are), you can use them as dict keys. Lists can't do this:

main.py
Output
Click Run to see the output here.

That pattern — keying by a composite of values — is surprisingly useful. Think: "the score for user X on day Y" or "the best route from A to B."

Tuples for Multiple Return Values

A function that needs to return more than one thing usually returns a tuple:

main.py
Output
Click Run to see the output here.

Python's own built-in divmod() does exactly this. The calling code's destructuring assignment makes it read naturally.

Named Tuples, When Position Isn't Enough

Once your tuples grow past two or three items, remembering which position means what becomes tedious. The collections.namedtuple fixes that:

main.py
Output
Click Run to see the output here.

Now the tuple's items have names. It's still immutable, still a tuple (so unpacking and indexing work), but the access pattern is a lot friendlier. For anything more complex, there's dataclasses — we'll meet those later in the classes chapter.

Tuple vs List: A Quick Decision Guide

  • Fixed shape, each position has meaning → tuple.
  • A collection that might grow or shrink → list.
  • Returning multiple values from a function → tuple.
  • Need a dict key or a set member → tuple (list won't work).
  • You're going to mutate it → list (tuple won't let you).

Moving On

Tuples cover one end of the spectrum: fixed, small, structured. Next up: sets, which cover a completely different job — fast membership checking and uniqueness.

Frequently Asked Questions

What is a tuple in Python?

A tuple is an ordered, immutable sequence — like a list, but once created you can't add, remove, or change its items. You write tuples with parentheses: point = (3, 4).

What's the difference between a tuple and a list?

Both are ordered sequences. Lists are mutable — you can change them after creation — while tuples are immutable. Tuples are typically used for small collections of values that belong together as a single "record," like coordinates or a function's multiple return values.

When should I use a tuple?

Use a tuple when you have a fixed-size record where each position has a meaning — coordinates, RGB colors, a database row. Also, use tuples as dictionary keys or set members, since lists can't be used there. For everything else where the collection might grow or shrink, lists are the default.

Learn to code with Coddy

GET STARTED