Python interview question

Data SciencePythonbasic

In Python, what is the main difference between a list and a tuple?

  1. AA list is immutable and hashable, while a tuple can be changed in place with methods such as append and remove.
  2. BA list can hold only one element type, while a tuple can mix element types such as ints, strings and floats.
  3. CA list is mutable and can be changed in place, while a tuple is immutable once it has been created.
  4. DA list keeps its elements in insertion order, while a tuple stores its elements unordered, like a set does.
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 append or remove methods.
  • 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.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Data Science questions

  1. An inventory agent asks an LLM for a JSON reorder decision and passes the fields to a typed helper. This time the model returned units as a string. What…
  2. The chart plots weekly active users of a new GenAI app on a log scale (the y-axis shows log10 of users). How should the change after week 10 be interpreted?
  3. A test ran at 10% treatment on Friday and was ramped to 50% for the weekend. Treatment converts better within each period but worse when the data are pooled…
  4. In PostgreSQL, converted is an INTEGER column holding 0 or 1. This daily conversion-rate query shows 0 for almost every day, and 1 only on days where every…
  5. A retailer's marketing mix model (MMM) transforms weekly TV spend with geometric adstock aₜ = xₜ + λ·aₜ₋₁, with λ = 0.6 and a₀ = 0, and then passes the…