Creating Tuples
Tuples use parentheses (optional but recommended). A single-element tuple requires a trailing comma.
# Creating tuples
t1 = (1, 2, 3)
t2 = "a", "b", "c" # Parentheses optional
t3 = (42,) # Single element - MUST have comma!
t4 = () # Empty tuple
print(t1) # (1, 2, 3)
print(t2) # ('a', 'b', 'c')
print(t3) # (42,)
print(type(t4)) # <class 'tuple'>
Accessing Tuple Elements
Same indexing and slicing as lists.
coords = (10, 20, 30)
print(coords[0]) # 10 - first element
print(coords[-1]) # 30 - last element
print(coords[1:]) # (20, 30) - slice
# But you CANNOT modify:
try:
coords[0] = 99
except TypeError as e:
print(f"Error: {e}")
Tuple Unpacking
Assign tuple elements to individual variables — a very Pythonic pattern.
# Basic unpacking
point = (4, 7)
x, y = point
print(f"x={x}, y={y}")
# Swap variables (uses tuple packing/unpacking)
a, b = 10, 20
a, b = b, a
print(f"a={a}, b={b}") # a=20, b=10
# Extended unpacking with *
first, *rest = (1, 2, 3, 4, 5)
print(first) # 1
print(rest) # [2, 3, 4, 5]
When to Use Tuples vs Lists
Use a tuple when data should not change. Use a list when you need to modify the collection.
# ✅ Tuples for fixed data
RGB_RED = (255, 0, 0)
DAYS_OF_WEEK = ("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
DATABASE_CONFIG = ("localhost", 5432, "mydb")
# ✅ Lists for mutable collections
cart_items = ["apple", "banana"]
cart_items.append("orange") # Can modify
# ✅ Tuples as dict keys (lists cannot be dict keys)
location_data = {(40.7128, -74.0060): "New York"}
Tuples: Immutable, Hashable, and Handy
A tuple is like a list but immutable — you can't add, remove, or change items after creation. That constraint is a feature: immutability makes tuples hashable, so they can be dict keys and set members (lists can't).
point = (3, 4)
locations = {(0, 0): "origin", (3, 4): "target"} # tuple as dict key ✅
# point[0] = 9 → TypeError: tuples are immutable
x, y = point # unpacking
a, *rest = (1, 2, 3) # a=1, rest=[2, 3]
The one-element gotcha
not_a_tuple = (5) # just int 5 — parentheses do nothing
a_tuple = (5,) # the trailing comma makes it a tuple
| list | tuple | |
|---|---|---|
| Mutable | yes | no |
| Dict key / set member | no | yes |
| Use for | homogeneous, growing data | fixed records, function returns |
Convention: tuples often model records (heterogeneous fields that belong together — a coordinate, an RGB color), while lists hold a variable number of similar items. Functions returning multiple values return a tuple. Note: immutable means the tuple can't be reassigned — but if it holds a mutable object (a list), that inner object can still change.
🏋️ Practical Exercise
Practice with tuples:
- Create a tuple of coordinates
(x, y)and access each element by index. - Unpack a tuple into separate variables in one line.
- Try to modify a tuple element and observe the
TypeError. - Return two values from a function and capture them via tuple unpacking.
🔥 Challenge Exercise
Write a function min_max(numbers) that returns both the smallest and largest values as a tuple, then unpack the result at the call site. Next, store a list of (name, age) tuples and sort it by age using a key function. Finally, demonstrate using a tuple as a dictionary key (e.g. a grid coordinate) to show why immutability matters.
📋 Summary
- A tuple is an ordered, immutable collection written with parentheses (or just commas).
- Tuples support indexing and slicing but cannot be modified after creation.
- Tuple unpacking assigns elements to multiple variables at once:
x, y = point. - A single-element tuple needs a trailing comma:
(5,). - Because they are immutable and hashable, tuples can serve as dictionary keys and set elements.
- Functions returning multiple values actually return a tuple, which callers can unpack.
Interview Questions on Tuples
- What is a tuple and how is it different from a list?
- Why are tuples immutable and when is that useful?
- What is tuple unpacking?
- How do you create a single-element tuple?
- Can a tuple be used as a dictionary key? Why or why not?
- When should you choose a tuple over a list?
- How do functions use tuples to return multiple values?
Related Topics
FAQ
A list is mutable — you can add, remove, and change elements. A tuple is immutable — once created it cannot change. This makes tuples slightly faster, safe to use as dict keys, and a good choice for fixed collections of related values.
Add a trailing comma: t = (5,). Without the comma, (5) is just the integer 5 in parentheses, not a tuple.
Dictionary keys must be hashable, and hashability requires immutability. Tuples (of hashable elements) are immutable and hashable; lists are mutable and therefore unhashable.
Use a tuple for a fixed, unchanging group of related values — coordinates, RGB colors, database records — or when you need a hashable key. Use a list when the collection will grow, shrink, or change.

