11.1 Introduction to Tuples
What a tuple is, its core characteristics, why immutability is a feature rather than a limitation, and how CPython allocates exactly enough memory for a tuple's fixed contents -- with no spare capacity to manage.
What Is a Tuple?
What Is It?
An ordered, immutable collection — like a list, but once created it can never be changed. Written with parentheses (optional, but conventional), comma-separated.
>>> point = (3, 4)
>>> point
(3, 4)
Why Does It Matter?
A tuple represents data that’s a fixed record or grouping that should never be accidentally modified — the opposite design goal from Chapter 10: Lists.
Characteristics
- Ordered — items keep their insertion position.
- Immutable — cannot be changed after creation (see 11.9 Immutability and Copying).
- Allows duplicates.
- Hashable (if all contents are hashable) — usable as a dict key or set member, unlike a list (see 5.9 Mutable vs Immutable Types).
Why Use Tuples?
Immutability is a feature, not a limitation: it guarantees a value can’t be accidentally changed elsewhere in a program, makes it safely shareable, and is exactly what’s required to use a compound value as a dict key.
Real-World Applications
- A coordinate pair
(x, y) - A database row/record returned from a query
- RGB color values
(255, 0, 0) - Function return values that bundle multiple results together
Tuples in DevOps
Fixed configuration values that should never change at runtime — AWS regions, allowed ports, server details — are naturally represented as tuples. 11.12 Tuples in DevOps is dedicated entirely to these patterns.
>>> DB_CONFIG = ("localhost", 5432, "mydb")
Internal Representation
A tuple’s size is known and fixed at creation, so CPython allocates exactly enough space — nothing extra.
Unlike a list (see 10.1 Introduction to Lists), a tuple’s exact size is known at creation time, so CPython allocates exactly enough space for its elements — no spare capacity reserved, because it can never grow.
>>> import sys
>>> sys.getsizeof((1, 2, 3))
64
>>> sys.getsizeof([1, 2, 3]) # same data, but list reserves extra room
88
Once built, a tuple’s contents — which object each slot references — can never be reassigned; attempting to do so raises TypeError.
>>> t = (1, 2, 3)
>>> t[0] = 99
Traceback (most recent call last):
TypeError: 'tuple' object does not support item assignment
Like a list, each tuple slot holds a reference to an object, not the object’s data directly — this is exactly what allows a tuple to contain a mutable object even though the tuple itself is immutable (see 11.8 Nested Tuples).
No spare capacity to manage, no possibility of resizing, and (for tuples of hashable items) a cached hash — all of which make tuples marginally faster to create and smaller in memory than an equivalent list; full comparison in 11.10 Performance.
Quick Interview Answer
“A tuple is an ordered, immutable collection — everything a list is, minus the ability to change after creation. That immutability isn’t a limitation, it’s the entire point: it makes a tuple safe to share without defensive copying, and it’s exactly what makes a tuple hashable and therefore usable as a dict key or set member, which a list can never be. Internally, because a tuple’s size is fixed and known at creation, CPython allocates exactly enough memory for it — no spare capacity the way a list reserves for future
append()calls — which is why a tuple is both smaller and marginally faster to create than an equivalent list.”
Common Mistakes
- Treating a tuple as “just an immutable list” without connecting why that matters — the practical payoff is hashability (dict keys, set members) and safety from accidental mutation, not just a syntax restriction.
- Assuming a tuple reserves spare capacity the way a list does — it doesn’t, and can’t, since it’s never going to grow.
- Reaching for a list by default even when the data is a fixed, unchanging record — a tuple documents that intent and is slightly cheaper.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form