In Python, what is the main difference between a list and a tuple?
Show the answer and explanation
Correct answer: C, A list is mutable and can be changed in place, while a tuple is immutable once it has been created.
Explanation Python
A list is mutable: you can append, remove or reassign elements in place. A tuple is immutable: after creation its elements cannot be added, removed or reassigned (t[0] = 9 raises a TypeError).
Why the other statements are wrong:
- The mutability is the other way round; tuples have no
appendorremovemethods. - Both lists and tuples can hold a mix of types, e.g.
[1, "a", 2.5]and(1, "a", 2.5). - Both are ordered sequences that support indexing and slicing; neither behaves like a set.
Practical consequences: a tuple whose elements are all hashable is itself hashable, so it can be a dict key or a set member (a list cannot). Note that immutability is shallow: (1, [2]) cannot be hashed, and the inner list can still be mutated.
Interview tip: say "list = mutable, tuple = immutable" first, then add the use cases: tuples for fixed records and dict keys, lists for collections that grow or change.