Beginner
Tuples and Their Immutability
📂 Phase 3: Lists, Tuples, Dictionaries, Sets (Days 11–15) · Python ProgrammingA tuple is an ordered collection almost identical to a list in how you create and access it — with one defining difference: once created, a tuple can never be changed. No adding, removing, or reassigning individual elements.
Creating a Tuple
coordinates = (10, 20)
colors = ("red", "green", "blue")
mixed = ("Vishwas", 22, True)
single_item = ("apple",) # the trailing comma is REQUIRED for a one-item tuple
Without the trailing comma, ("apple") is just the string "apple" wrapped in ordinary parentheses, NOT a tuple. The comma — not the parentheses — is what actually makes it a tuple.
Accessing Tuple Items
Indexing and slicing work exactly the same way as lists.
colors = ("red", "green", "blue")
print(colors[0]) # red
print(colors[-1]) # blue
print(colors[0:2]) # ('red', 'green')
Tuples Cannot Be Modified
colors = ("red", "green", "blue")
# colors[0] = "yellow" # TypeError: 'tuple' object does not support item assignment
# colors.append("yellow") # AttributeError: tuples have no append() method
Lists vs Tuples — Side by Side
| Aspect | List | Tuple |
|---|---|---|
| Syntax | Square brackets [] | Parentheses () |
| Mutable? | Yes — can add, remove, change | No — fixed once created |
| Performance | Slightly slower | Slightly faster, due to immutability |
| Usable as a dict key? | No — lists are unhashable | Yes, if all elements are themselves immutable |
| Typical use case | A collection that will change over time | A fixed record, like coordinates or RGB values |
Tuple Unpacking
One of the most useful tuple features — assigning multiple variables from a tuple in a single line.
point = (4, 7)
x, y = point
print(x) # 4
print(y) # 7
# This is exactly how a function returns multiple values
def get_min_max(numbers):
return min(numbers), max(numbers)
low, high = get_min_max([3, 8, 1, 9])
print(low, high) # 1 9
Unpacking With *
numbers = (1, 2, 3, 4, 5)
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4] — collected into a list
print(last) # 5
Why Use a Tuple Instead of a List?
- Data integrity: guarantees the data can never accidentally be modified elsewhere in the program
- Dictionary keys: tuples (unlike lists) can be used as dictionary keys, since they are hashable
- Performance: tuples have a smaller memory footprint and are marginally faster to iterate over than lists
# A tuple as a dictionary key — only possible because tuples are immutable
locations = {
(12.97, 77.59): "Bengaluru",
(28.61, 77.21): "Delhi"
}
print(locations[(12.97, 77.59)]) # Bengaluru
Converting Between Lists and Tuples
my_list = [1, 2, 3]
my_tuple = tuple(my_list) # (1, 2, 3)
back_to_list = list(my_tuple) # [1, 2, 3]
Interview tip: "Why would you ever choose a tuple over a list?" is a near-guaranteed question — the strongest answer combines all three reasons: immutability for data safety, hashability for use as dict keys, and the minor performance edge, rather than giving just one.