ReviseAlgo Logo

Tuples

Tuple Basics

Creating and using tuples

Interview: Tests understanding of immutability and Python data model

Last Updated: June 12, 2026 9 min read

Tuples are immutable, ordered, heterogeneous sequences. Once created, their elements cannot be changed. This immutability makes them hashable (usable as dictionary keys) and gives them performance advantages over lists.

Creating Tuples

  • Literal: (1, 2, 3) — parentheses optional but recommended
  • Single element: (1,) — the trailing comma is REQUIRED. (1) is just the integer 1
  • Empty: () or tuple()
  • Packing: t = 1, 2, 3 — creates tuple without parentheses
  • Constructor: tuple([1, 2, 3]) — from any iterable

Immutability

  • Cannot modify elements: t[0] = 10 raises TypeError
  • Nested mutable objects: A tuple containing lists is "shallowly immutable" — the tuple's references can't change, but the mutable objects inside can
  • Hashable: Tuples of immutable elements can be dict keys and set members
  • Thread-safe: Immutable objects don't need locks for concurrent access

Performance

  • Tuples use less memory than lists (no overallocation needed)
  • Creation is faster — Python can cache small tuples internally
  • Indexing is equally fast O(1) for both tuples and lists

Common Pitfall

(1) is the integer 1, not a tuple. You must write (1,) for a single-element tuple. This is the most common tuple mistake in Python.

Use Cases

Representing fixed collections (coordinates, RGB colors, database rows)

Dictionary keys (when you need composite keys)

Returning multiple values from functions

Named tuples for lightweight record types

Common Mistakes

Forgetting the comma for single-element tuples: (1) is int, (1,) is tuple

Thinking tuples are fully immutable — nested mutable objects CAN be modified

Using tuples when you need to modify elements (use a list instead)

Not knowing that parentheses are optional for tuple creation