Tuples & Named Tuples Interview Questions & Answers
5 questions Updated 2026-06-18
Python interview questions on tuples vs lists, packing and unpacking, single-element tuples, shallow immutability, namedtuples, and tuples as dict keys.
A list is mutable and variable-length — use it for a homogeneous,
changing collection you add to, remove from, or reorder. A tuple is
immutable and fixed — use it for a heterogeneous, fixed record whose
shape won't change, like a coordinate or a database row.
scores = [90, 85, 70] # changing collection -> list
scores.append(60) # fine
point = (3, 4) # fixed record -> tuple
point[0] = 9 # TypeError — tuples are immutable
Tuples are also slightly faster and more memory-efficient, and because they're hashable they can serve as dict keys or set members (a list can't). Rule of thumb: reach for a tuple when the data is a fixed bundle that shouldn't change, and a list when you need to mutate the collection.
Packing collects several values into one tuple — the parentheses are often optional. Unpacking spreads a tuple's items into separate names in one assignment, which is how you return and receive multiple values cleanly.
t = 1, 2, 3 # packing (parentheses optional)
a, b, c = t # unpacking -> a=1, b=2, c=3
first, *rest = t # extended unpacking -> first=1, rest=[2, 3]
one = (5) # NOT a tuple — just int 5 in parentheses
one = (5,) # a 1-element tuple — the trailing comma makes it
The key gotcha is the single-element tuple: it's the trailing comma, not
the parentheses, that creates a tuple — (5) is just the integer 5, while
(5,) is a one-tuple. When in doubt, the comma is what defines a tuple.
A tuple's immutability is shallow. You can't reassign or resize its slots, but each slot just holds a reference — and if that reference points to a mutable object (like a list), that object can still be changed in place.
t = (1, [2, 3])
t[1].append(4) # allowed — mutating the list inside the tuple
print(t) # (1, [2, 3, 4])
t[1] = [9] # TypeError — can't reassign a tuple slot
A consequence interviewers love: a tuple that contains a list is not
hashable, because hashability requires every element to be immutable too —
so hash((1, [2])) raises TypeError. Bottom line: the tuple's structure is
frozen, but the objects it points to may not be.
A namedtuple is an immutable tuple subclass with named fields — giving you readable, hashable, lightweight records that still behave like tuples (index access, unpacking, comparison). There are two ways to define one.
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
p.x, p[0] # 3, 3 — named AND index access
p.x = 9 # AttributeError — immutable
from typing import NamedTuple
class Point(NamedTuple): # class syntax with type hints + defaults
x: int
y: int = 0
The collections form is concise; the typing.NamedTuple class form adds
type annotations, defaults, and the ability to add methods. Use a namedtuple
for small fixed records when you want clarity over a bare tuple; reach for a
@dataclass when you need mutability or richer behavior.
Dict keys and set members must be hashable, which in practice means immutable with a stable hash for the object's lifetime. A tuple of immutable values is hashable, so it works as a key; a list is mutable and unhashable, so it doesn't.
grid = {}
grid[(0, 0)] = "origin" # tuple key — works
grid[(1, 2)] = "point"
grid[[3, 4]] = "x" # TypeError: unhashable type: 'list'
This makes tuples ideal for composite / multi-part keys — like (row, col)
coordinates or (lat, lon) pairs. The one caveat: a tuple is only hashable if
all its contents are too, so (1, [2]) still fails. Use an immutable tuple
whenever you need a multi-value key.
Practice tests are coming soon
Get notified when interactive mock interviews and quizzes launch.