Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Data types & structuresJuniorVery commonTheory

When should you choose a tuple instead of a list?

Answer

Choose a tuple for a fixed-size, heterogeneous, read-only record — such as a coordinate pair or a database row — where immutability communicates intent and prevents accidental mutation, and choose a list for a homogeneous, variable-length collection you plan to grow, shrink or reorder. Tuples are also hashable when their elements are hashable, so they can be used as dict keys or set members, while lists cannot.

Strong answer includes

  • tuple for fixed-shape immutable records
  • list for growable homogeneous collections
  • tuples are hashable and can be dict keys

Practical example

Python example
point = (12.4, 55.1)

signals a fixed 2D coordinate that should not change shape, while

Python example
queue = []

signals a collection meant to be appended to and popped from over time.

#lists#tuples

Sources

The Python Standard LibraryPython Software Foundation · Authoritative behavior for built-in types and standard library modulesThe Python TutorialPython Software Foundation · Baseline syntax, control flow, functions and module basics