Tuples — Immutable Ordered Collections
A **tuple** is an **ordered**, **immutable** sequence of elements in Python.
Chapter 3: Data Structures
Sub-chapter: Tuples — Immutable Ordered Collections
A tuple is an ordered, immutable sequence of elements in Python.
Unlike lists, tuples cannot be changed after creation — no additions, deletions, or updates.
This immutability makes them fast, memory-efficient, and safe for storing constant data.
🧠 Why Use Tuples?
Tuples are ideal when:
- ✅ You want data integrity (unchangeable after creation)
- ⚡ You need faster performance than lists
- 🧱 You need to use them as dictionary keys or set elements
- 📦 You’re returning multiple values from a function
⚙️ Creating Tuples
Tuples can be created with parentheses () or the tuple() constructor.
person = ("Alice", 30, "New York")
numbers = (1, 2, 3, 4, 5)
empty = ()
mixed = ("Python", 3.12, True)
You can also create tuples without parentheses — commas alone are enough:
coords = 10, 20, 30
print(type(coords)) # <class 'tuple'>
🧩 Single-Element Tuples
For a one-element tuple, add a trailing comma — otherwise it’s not recognized as a tuple.
single = ("Hello",)
not_tuple = ("Hello")
print(type(single)) # tuple
print(type(not_tuple)) # str
🎯 Accessing Elements
Tuples are zero-indexed like lists.
person = ("Alice", 30, "New York")
print(person[0]) # Alice
print(person[-1]) # New York
You can also slice tuples:
nums = (10, 20, 30, 40, 50)
print(nums[1:4]) # (20, 30, 40)
🧩 Tuple Unpacking
Tuple unpacking allows you to assign multiple values in one statement.
person = ("Alice", 30, "New York")
name, age, city = person
print(name, age, city)
Using the * (asterisk) operator for extended unpacking:
a, *b = (1, 2, 3, 4)
print(a) # 1
print(b) # [2, 3, 4]
🧮 Tuples as Multiple Return Values
Tuples are commonly used to return multiple results from functions.
def get_position():
return 12, 8
x, y = get_position()
print(x, y) # 12 8
🧱 Tuple Methods
Because tuples are immutable, they have only two methods:
| Method | Description | Example |
|---|---|---|
count(value) | Counts occurrences of a value | (1,2,2,3).count(2) → 2 |
index(value) | Finds index of first occurrence | (1,2,3).index(3) → 2 |
⚡ Tuples vs Lists
| Feature | Tuple | List |
|---|---|---|
| Syntax | (1, 2, 3) | [1, 2, 3] |
| Mutable | ❌ No | ✅ Yes |
| Speed | ⚡ Faster | 🐢 Slower |
| Memory | 🧠 Lower | 🧠 Higher |
| Hashable | ✅ Yes | ❌ No |
| Use Case | Fixed data, dictionary keys | Dynamic or changing data |
Performance Comparison Example
import sys, timeit
list_obj = [1, 2, 3, 4, 5]
tuple_obj = (1, 2, 3, 4, 5)
print(sys.getsizeof(list_obj)) # e.g., 104 bytes
print(sys.getsizeof(tuple_obj)) # e.g., 88 bytes
print(timeit.timeit(lambda: (1,2,3,4,5), number=1_000_000))
print(timeit.timeit(lambda: [1,2,3,4,5], number=1_000_000))
Tuples are lighter and faster to create than lists.
🧩 Nested Tuples
Tuples can contain other tuples (or any data type).
matrix = (
(1, 2, 3),
(4, 5, 6),
(7, 8, 9)
)
print(matrix[1][2]) # 6
🧰 Tuples as Dictionary Keys
Because tuples are immutable and hashable, they can be used as keys in dictionaries.
locations = {}
coords = (35.6895, 139.6917)
locations[coords] = "Tokyo"
print(locations)
🧱 Named Tuples (Structured Tuples)
Tuples can be given named fields using namedtuple from the collections module — combining immutability with readability.
from collections import namedtuple
Person = namedtuple("Person", ["name", "age", "city"])
p = Person("Alice", 30, "New York")
print(p.name, p.age, p.city)
Output:
Alice 30 New York
🧠 Common Tuple Use Cases
- Returning multiple values from a function
- Representing fixed configurations (coordinates, RGB, date/time)
- Using as dictionary keys or set members
- Storing structured data safely
- Iterating paired data using
zip()
Example:
names = ("Alice", "Bob", "Charlie")
ages = (25, 30, 35)
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")
⚙️ When to Use Tuples vs Lists
Use tuples when:
- Data should never change
- You need immutable dictionary keys
- You want faster, memory-efficient storage
Use lists when:
- Data will change over time
- You need to add/remove items dynamically
🧾 Key Takeaways
- Tuples are immutable ordered collections.
- They are faster and lighter than lists.
- They’re hashable and safe for fixed data.
- Useful for multiple returns, keys, and data integrity.
- Enhance readability with namedtuples when working with structured records.
Tuples combine speed, safety, and structure — the perfect choice when your data should stay constant.