Skip to main content

Tuple

Overview

A Tuple is an abstract data type that groups a fixed number of elements into a single, ordered collection. Unlike lists, tuples usually have a fixed size and may contain elements of different types.

Tuples emphasize structure over mutability.


What Is a Tuple?

A tuple:

  • Has a fixed length
  • Preserves order
  • Allows heterogeneous elements
  • Is often immutable

Each position has a defined meaning.


How It Works

Tuples are typically implemented as:

  • Fixed-size arrays
  • Lightweight records

Access is positional:

tuple[0], tuple[1], ...

Example

Pseudocode

point = (10, 20)
nameAge = ("Ravi", 25)

Real-world Analogy

A tuple is like a form with numbered fields. Each slot has a purpose, and the order cannot be changed 🔢🗂️.


Common Operations

  • Construction
  • Destructuring
  • Index-based access
  • Comparison (lexicographical)

Time and Space Complexity

  • Space: O(n)
  • Access: O(1)

Use Cases

  • Returning multiple values from functions
  • Lightweight data grouping
  • Coordinates and pairs
  • Pattern matching

Advantages

  • Compact representation
  • Clear structure
  • Safe from accidental modification

Limitations

  • Fixed size
  • Less expressive than records
  • Position-based access can reduce readability

Tuple vs List

AspectTupleList
SizeFixedDynamic
MutabilityOften immutableMutable
TypesHeterogeneousUsually homogeneous