Difference Between List and Tuple in Python
Understand the difference between a list and a tuple in Python: mutability, syntax, methods, hashability, performance, examples, and common interview questions.
Expected Interview Answer
The core difference is mutability: a list is mutable and can be changed after creation, while a tuple is immutable and cannot be modified once created. Lists use square brackets and tuples use parentheses.
Because tuples are immutable, they are hashable and can serve as dictionary keys or set elements, and they are slightly faster and more memory-efficient than lists. Lists offer many in-place methods such as append, remove, and sort, whereas tuples expose only count and index. Use a list for a changing collection of similar items and a tuple for a fixed record of related values.
- Tuples guarantee data cannot be accidentally changed
- Tuples are hashable, so they can be dictionary keys
- Tuples use less memory and can be faster to create
- Lists provide rich in-place editing methods
- Choosing the right one signals intent to other developers
AI Mentor Explanation
A list is like a batting order chalked on a whiteboard — the captain can swap positions, drop a player, or add a night-watchman right up to the toss. A tuple is like the final printed scorecard after the match: the runs and dismissals are fixed and cannot be edited, so it can be safely filed and referenced as an official record.
Step-by-Step Explanation
Step 1
Syntax difference
Lists use square brackets [1, 2, 3]; tuples use parentheses (1, 2, 3) or just commas.
Step 2
Mutability
Lists can be changed in place; tuples cannot be modified after creation.
Step 3
Available methods
Lists offer append, insert, remove, sort, and more; tuples only expose count and index.
Step 4
Hashability
Tuples of hashable items can be dictionary keys or set members; lists cannot.
Step 5
Choose intentionally
Use a list for a growing homogeneous collection, a tuple for a fixed record of related fields.
What Interviewer Expects
- States mutability as the primary difference
- Knows the bracket vs parenthesis syntax
- Explains why tuples can be dictionary keys
- Mentions performance and memory trade-offs
- Gives a sensible rule of thumb for choosing one
Common Mistakes
- Saying tuples can be modified after creation
- Believing a single-element tuple is (x) instead of (x,)
- Thinking lists can be used as dictionary keys
- Assuming immutability means a tuple can never contain a mutable object
Best Answer (HR Friendly)
“A list and a tuple both store a group of items, but a list can be changed later while a tuple is locked once you create it. You use a list when the data will change, like a shopping cart, and a tuple when it should stay fixed, like a set of coordinates.”
Code Example
# List: mutable
fruits = ["apple", "banana"]
fruits.append("cherry") # OK
fruits[0] = "avocado" # OK
print(fruits) # ['avocado', 'banana', 'cherry']
# Tuple: immutable
point = (3, 4)
# point[0] = 9 # TypeError: 'tuple' object does not support item assignment
# Because tuples are hashable, they can be dict keys
locations = {(3, 4): "start", (7, 1): "goal"}
print(locations[(3, 4)]) # start
# A one-element tuple needs the trailing comma
single = (5,)
print(type(single).__name__) # tupleFollow-up Questions
- Why can a tuple be used as a dictionary key but a list cannot?
- How do you create a tuple with a single element?
- Are tuples always faster than lists, and why?
- Can a tuple contain a mutable object like a list?
- When would you convert a list to a tuple or vice versa?
MCQ Practice
1. What is the main difference between a list and a tuple?
The defining difference is mutability: lists can be changed after creation, tuples cannot.
2. Which of these can be used as a dictionary key?
A tuple of hashable elements is itself hashable, so (1, 2) can be a dictionary key; a list is unhashable.
3. How do you correctly create a single-element tuple?
Without the trailing comma, (5) is just the integer 5 in parentheses; (5,) is a one-element tuple.
Flash Cards
List vs tuple in one word? — Mutability — lists are mutable, tuples are immutable.
Which methods do tuples have? — Only count() and index(); lists add append, remove, sort, and more.
Why can tuples be dict keys? — Immutable tuples of hashable items are hashable, so they work as keys; lists are not.
How to write a one-element tuple? — Add a trailing comma: (5,). Without it, (5) is just the integer 5.