Tuples in Python#
š Data Analysis Using Python š Strings & Data Structures Lesson 021
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Ordered and unchangeable#
A tuple is like a list ā an ordered collection of values ā but with one crucial difference: it is immutable, unchangeable after creation. Tuples serve where a collection should be fixed, and understanding them (and why immutability is sometimes wanted) rounds out the sequence structures. This lesson covers tuples.
Creating and using tuples#
A tuple is written with parentheses (or just commas):
point = (3, 5)
rgb = (255, 128, 0)
record = ("North", 1000, 2024) # a mixed tuple
Tuples are accessed exactly like lists ā by index, with slicing:
point[0] # 3
record[1] # 1000
record[-1] # 2024
len(record) # 3
Everything about reading a tuple mirrors a list; the difference is entirely in changing it.
Immutability#
A tuple cannot be changed after creation ā no adding, removing, or updating elements:
point = (3, 5)
point[0] = 10 # ERROR ā tuples do not support item assignment
This immutability is the tupleās defining property, and it is the same immutability strings have. Where a list is a changeable ordered collection, a tuple is a fixed one ā the two are otherwise similar.
Why use a tuple#
If tuples are just unchangeable lists, why use them? Immutability is a feature in the right situations:
Fixed data that should not change ā coordinates, RGB colours, a fixed record ā where accidental modification would be a bug. The immutability protects the data.
Meaning and intent ā using a tuple signals āthis collection is fixed,ā documenting intent to readers.
Dictionary keys ā tuples can serve as dictionary keys (the next lessons) where lists cannot, precisely because they are immutable and stable.
Multiple return values ā functions often return several values as a tuple (
return x, y), a common Python idiom.
The choice between list and tuple is the choice between changeable and fixed: use a list when the collection will change, a tuple when it should not.
The caveat#
The tuple-versus-list choice is easy to get wrong in either direction: using a tuple for data that does need to change forces awkward workarounds (you cannot modify it), while using a list for data that should be fixed forgoes the protection immutability gives. The guidance is intent-based ā will this collection change during its life? Changeable ā list; fixed ā tuple. And a subtle trap: a tupleās immutability is shallow ā a tuple cannot be reassigned, but if it contains a mutable object (a list inside a tuple), that inner object can still change. For the flat collections of typical data work this rarely bites, but it is worth knowing that immutability applies to the tupleās own structure, not necessarily to everything within it. The next lesson combines the structures with loops and introduces list comprehension.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/tuples-in-python/ (insightful-data-lab.com).