ReviseAlgo Logo

Tuples

Tuple Operations

Accessing and unpacking tuples

Interview: Tests understanding of sequence protocol and tuple-specific constraints

Last Updated: June 12, 2026 7 min read

Tuples support all the standard sequence operations (indexing, slicing, concatenation, repetition, membership) but have only two methods: count() and index(). This minimal API is intentional — tuples are designed to be simple, immutable records.

Tuple Methods

  • count(x): Count occurrences of x — O(n)
  • index(x): Find index of first occurrence — O(n). Raises ValueError if not found
  • No modification methods (append, remove, etc.) — tuples are immutable

Sequence Operations

  • Concatenation (+): Creates a new tuple: (1,2) + (3,4) → (1,2,3,4)
  • Repetition (*): (1,2) * 3 → (1,2,1,2,1,2)
  • Membership (in): O(n) scan: 3 in (1,2,3) → True
  • Slicing: Returns a new tuple: (1,2,3,4)[1:3] → (2,3)
  • Comparison: Lexicographic, element-by-element
  • Built-ins: len(), min(), max(), sum(), sorted() all work

Conversion

  • To list: list(t) — when you need to modify
  • From list: tuple(lst) — when you want immutability
  • Common pattern: convert to list, modify, convert back to tuple

Use Cases

Returning multiple values from functions

Pairing data for parallel iteration

Composite sorting keys

Immutable records in collections

Common Mistakes

Trying to call sort() on a tuple (use sorted() which returns a list)

Forgetting that index() raises ValueError when element is not found

Using tuple repetition (*) with mutable elements (same shared-reference issue as lists)

Not knowing that + creates a new tuple (doesn't modify the original)