Skip to main content

Python Tuples

Satellite · Data Types

Python Tuples

Tuples give you fixed-length, read-only sequences—perfect for coordinates, return values, and dictionary keys.

Creation​

coords = (41.9, 12.5)
single = ("only",) # trailing comma required — ("only") is just a string
implicit = 1, 2, 3 # parentheses are optional
empty = ()

The comma—not the parentheses—makes a tuple. That single-element trailing comma is the most common tuple gotcha.

Tuples are immutable​

Once created, a tuple can't be changed—no append, no item assignment:

coords[0] = 0.0   # TypeError: 'tuple' object does not support item assignment

This immutability is what lets tuples be hashable, so they can serve as dictionary keys and set elements:

routes = {(41.9, 12.5): "Rome", (48.8, 2.3): "Paris"}

Note: a tuple is only hashable if all its elements are. A tuple containing a list is not.

Indexing and slicing​

Tuples are sequences, so they index and slice exactly like lists (the result is a new tuple):

t = (10, 20, 30, 40)
t[0] # 10
t[-1] # 40
t[1:3] # (20, 30)
len(t) # 4
20 in t # True

The only two methods are t.count(x) and t.index(x)—everything else would mutate.

Tuple unpacking​

x, y = coords
user_id, role, *extras = payload # * captures the rest into a list
a, b = b, a # swap without a temp variable

Unpacking is why functions can appear to "return multiple values"—they return one tuple that you unpack at the call site.

Named tuples​

When positional fields get hard to read, namedtuple adds names while staying immutable and tuple-compatible:

from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(41.9, 12.5)
p.x # 41.9 — access by name
p[0] # 41.9 — still works by index

Tuples vs lists​

FeatureTupleList
MutabilityImmutableMutable
HashableYes (if members hashable)No
Syntax(1, 2)[1, 2]
Use casesKeys, fixed records, function returnsDynamic collections

Next up in your learning path

Frequently Asked Questions

What is the difference between a tuple and a list?

A tuple is immutable and a list is mutable. Use a tuple for fixed collections—coordinates, records, function return values—and when you need a hashable value for a dict key or set element. Use a list when the collection changes over time.

How do I make a tuple with one element?

Add a trailing comma. Without the comma, the parentheses do nothing. The comma, not the parentheses, is what creates the tuple.

single = ("only",)   # tuple of length 1
not_tuple = ("only") # just the string "only"
type(("only",)) # <class 'tuple'>

Can I change a tuple after creating it?

No. Tuples do not support item assignment or methods like append. To get a modified version, build a new tuple. If a field contains a mutable object like a list, that inner object can still be changed.

old = (1, 2, 3)
old[0] = 9 # TypeError
new = old[:2] + (99,) # (1, 2, 99) — a new tuple

Why would I use a tuple as a dictionary key?

Because tuples are hashable, a tuple like (row, col) makes a natural composite key for grids, coordinates, or multi-field lookups. Lists cannot be keys because they are mutable and unhashable.

grid = {(0, 0): "start", (1, 2): "goal"}
grid[(1, 2)] # 'goal'