Description

A tuple is an immutable, ordered collection of elements in programming, meaning that once created, its contents cannot be changed. Tuples are a fundamental data structure in many programming languages such as Python, Haskell, and Scala. They are often used to group related values into a single compound object without the overhead of creating a full class or struct.

Unlike lists or arrays, which are typically mutable (modifiable), tuples provide a fixed-size, fixed-content container, making them suitable for representing records, coordinate pairs, or heterogeneous data.

Characteristics of Tuples

FeatureDescription
OrderedElements have a specific sequence
ImmutableElements cannot be changed after creation
HeterogeneousCan contain elements of different types
IndexedSupports access by position
Fixed LengthSize is determined at creation, cannot grow

Tuple vs List (Python Example)

AspectTupleList
MutabilityImmutableMutable
Syntax(1, 2, 3)[1, 2, 3]
Use CaseFixed records, keys in dictDynamic collections
PerformanceSlightly faster due to immutabilitySlightly slower
MethodsLimited (count, index)Extensive (append, remove)

Creating Tuples

In Python:

# Empty tuple
empty = ()

# Single element tuple (comma is mandatory)
single = (42,)

# Multiple elements
coordinates = (10.0, 20.0)

# Mixed types
person = ("Alice", 30, True)

Accessing Tuple Elements

  • By indexing: person[0]"Alice"
  • By slicing: coordinates[0:1](10.0,)
  • Nested tuples supported

Tuple Unpacking

A powerful feature that assigns tuple elements to variables:

x, y = (10, 20)
name, age, is_employee = ("Bob", 25, False)

Allows cleaner and more readable code.

Use Cases of Tuples

  • Returning multiple values from functions
  • Immutable records that should not change
  • Dictionary keys (since keys must be immutable)
  • Data grouping in heterogeneous datasets
  • Passing fixed configurations

Tuple Operations and Methods (Python)

  • len(tuple): Length
  • tuple.count(value): Count occurrences
  • tuple.index(value): Find index of first occurrence
  • Concatenation: t1 + t2
  • Repetition: t1 * 3
  • Membership: value in tuple

Immutability Benefits

  • Thread-safe by default (no side effects)
  • Can be used as dictionary keys or in sets
  • Prevents accidental modification
  • Potential performance optimizations

Differences in Other Languages

  • Haskell: Tuples are fixed-size heterogeneous collections
  • Scala: Supports tuples of different arities, used heavily in functional programming
  • Java: No native tuples, but often emulated with Pair or custom classes
  • C++: std::tuple supports heterogenous collections

Tuple vs Struct vs Class

StructureMutabilityNamed fieldsUse Case
TupleImmutableNoGrouping fixed elements
StructMutableYesSimple data containers
ClassMutableYesComplex data and behavior

Example: Returning Multiple Values from a Function

def get_user():
    return ("Alice", 25, "Engineer")

name, age, profession = get_user()
print(name)  # Alice

Related Terms

  • List
  • Array
  • Record
  • Immutable Data Structures
  • Tuple Unpacking
  • NamedTuple
  • Dataclass
  • Hashable
  • Sequence
  • Collection