KatInterview

Python Lists vs Tuples: Mutability, Memory, and Use Cases

StringsModel answer
MediumModel answerStringspythondata-structuresmutability

Last updated

Compare Python lists and tuples across mutability, memory layout, hashability, and idiomatic use, including tricky edge cases.

Python provides two built-in sequence types that look similar on the surface but behave quite differently in practice: list and tuple. Both are ordered, both support indexing and slicing, and both can hold any mix of object types. Yet experienced Python developers choose between them deliberately.

Explain the key differences between a list and a tuple. Your answer should cover:

  • Mutability — what it means, what operations it enables or forbids, and why immutability being shallow matters
  • Performance and memory — at a high level, how each type allocates memory and where speed differences come from
  • Typical use cases — when you would reach for each one, and a useful heuristic for deciding
  • Hashability — why tuples can serve as dictionary keys but lists cannot, and under what conditions a tuple itself is not hashable
  • At least one code snippet that illustrates a meaningful or surprising difference

Note any gotchas or edge cases a working Python programmer should know.

Hints

Hint 1

Ask yourself: what single property explains almost every other difference between the two types — hashability, memory layout, safe use as constants, all of it?

Hint 2

Think about what 'shallow immutability' means. A tuple cannot change which objects it references, but can those objects change? What does that imply for hashability of a tuple containing a list?

Hint 3

Consider the += operator on both types. For a list, does it mutate in place or create a new object? For a tuple? What happens to other variables that reference the same object?