List vs Tuple: What is the Difference?
Understand the real difference between Python lists and tuples - mutability, hashability, performance, and when to use each, with clear code examples.
Expected Interview Answer
A list is a mutable, ordered collection you can grow, shrink, or modify in place, while a tuple is an immutable, ordered collection whose contents are fixed once created — that single mutability difference drives every other practical distinction between them.
Because lists are mutable, they support methods like append, remove, and sort that alter the object in place, and they are unhashable so they cannot be dict keys or set members. Tuples, being immutable, are hashable when their elements are hashable, which makes them usable as dict keys and set elements, and they are typically used for fixed, heterogeneous records like coordinates or return values from a function. Tuples also have a small performance and memory edge since Python can allocate them more compactly and CPython caches small tuples. Syntactically both use similar indexing and slicing, but tuples are written with commas (parentheses are optional except for the empty tuple) while lists require square brackets.
- Lists: flexible, in-place mutation for growing collections
- Tuples: hashable, so usable as dict keys and set members
- Tuples signal 'this data is fixed' to future readers
- Tuples have a minor memory/speed advantage from immutability
- Both support the same indexing, slicing, and iteration
AI Mentor Explanation
A tuple is like the toss result and playing XI submitted to the umpire before the match starts — fixed for the day, no substitutions allowed once play begins. A list is like the over-by-over bowling changes a captain makes, freely rearranged as the innings unfolds based on how the game is going.
Step-by-Step Explanation
Step 1
Mutability
Lists can be changed after creation (append, remove, sort); tuples cannot be altered once created.
Step 2
Syntax
Lists use square brackets [1, 2, 3]; tuples use commas, conventionally with parentheses (1, 2, 3).
Step 3
Hashability
Tuples of hashable elements can be dict keys or set members; lists never can.
Step 4
Performance
Tuples are slightly faster to create and iterate, and use less memory, due to their fixed size.
Step 5
Intent signaling
Tuples communicate 'fixed record'; lists communicate 'growing/changing collection' to future readers.
What Interviewer Expects
- Leads with mutability as the core difference
- Knows tuples are hashable and can be dict keys; lists cannot
- Can cite a real use case for each (e.g., coordinates vs a queue)
- Mentions the minor performance/memory advantage of tuples
- Does not claim tuples are always faster in every scenario
Common Mistakes
- Saying the only difference is bracket type vs parentheses
- Claiming tuples can be modified with += without knowing it creates a new object
- Forgetting that a tuple containing a list is still technically mutable via that nested list
- Not knowing a single-element tuple needs a trailing comma: (1,)
Best Answer (HR Friendly)
“A list is a flexible collection you can keep changing, like a to-do list you edit all day. A tuple is a fixed collection you set once and never change, like a birthdate — useful when you want to guarantee the data stays exactly as recorded.”
Code Example
coords = (10, 20) # tuple: fixed point
queue = [1, 2, 3] # list: growing collection
queue.append(4) # OK, lists are mutable
print(queue) # [1, 2, 3, 4]
try:
coords[0] = 99 # TypeError: tuples are immutable
except TypeError as e:
print("Error:", e)
cache = {coords: "origin-ish"} # tuples can be dict keys
print(cache) # {(10, 20): 'origin-ish'}Follow-up Questions
- Can a tuple contain a mutable object like a list?
- Why are tuples hashable but lists are not?
- When would you prefer a namedtuple over a plain tuple?
- How does tuple unpacking work in function returns?
- Is tuple creation actually faster than list creation, and why?
MCQ Practice
1. Which collection type can be used as a dictionary key?
Tuples made of hashable elements are themselves hashable, so they can be used as dict keys; list, set, and dict cannot.
2. What happens when you try to reassign an element of a tuple?
Tuples are immutable, so item assignment raises TypeError: 'tuple' object does not support item assignment.
3. Which is the correct syntax for a single-element tuple?
A trailing comma is required; (5) is just the integer 5 in parentheses, while (5,) is a one-item tuple.
Flash Cards
Core difference between list and tuple? — Lists are mutable; tuples are immutable.
Which is hashable, list or tuple? — Tuple (if its elements are hashable) — lists are never hashable.
Syntax for an empty tuple vs empty list? — Empty tuple: (); empty list: [].
Why use a tuple instead of a list? — To signal fixed, unchanging data and to allow use as a dict key or set member.