← Master Index
Vol. 03 Module 3.1 Lecture

Tuple

Python Basics

How This Lesson Fits the Module

Lists are mutable; tuples are immutable ordered sequences. Immutability guarantees data cannot change accidentally—critical for hashable records used as dictionary keys, fixed dataset schemas, and coordinates returned from functions.

In AI pipelines, tuples represent things that must not drift: a (height, width) image shape, a (mean, std) normalization pair, or a labeled sample (features_tuple, label) stored in a set for deduplication.

Learning Objectives

By the end of this lesson, students should be able to:

  • Create tuples with parentheses or trailing commas.
  • Explain immutability and its implications for hashing and safety.
  • Access tuple elements by index and slice like lists.
  • Unpack tuples into multiple variables.
  • Use tuples as dictionary keys and function return bundles.
  • Choose tuple vs list for fixed-schema AI records.

Creating Tuples

Definition — Tuple

A tuple is an ordered, immutable sequence of objects, written with parentheses (or commas alone). Once created, elements cannot be added, removed, or reassigned. Immutability makes tuples hashable when all elements are hashable.

# Image dimensions — fixed (height, width, channels) image_shape = (224, 224, 3) # Normalization stats returned from fit() norm_stats = (0.485, 0.229) # mean, std for one channel # Single-element tuple needs trailing comma singleton = (42,) # Tuple from list — freeze mutable data feature_names = tuple(["sq_ft", "bedrooms", "age"])

Immutability in Practice

List (mutable)

  • losses.append(0.3) OK
  • Cannot use as dict key
  • Grows during training logs
  • Flexible, in-place updates

Tuple (immutable)

  • shape[0] = 256 raises TypeError
  • Hashable if elements are hashable
  • Fixed records and coordinates
  • Signals “do not modify”
# Tuple as dict key — cache computed results by input shape cache = {} input_shape = (32, 128) # (batch_size, feature_dim) cache[input_shape] = "precomputed_projection_matrix" print(cache[(32, 128)]) # hit cache # Attempt to mutate — fails # input_shape[0] = 64 # TypeError: 'tuple' object does not support item assignment

Unpacking and Returning Tuples

Functions often return multiple values as a tuple, unpacked at the call site. This pattern is ubiquitous in train/validation splits and min-max computations.

Pattern Example AI Use
Return bundle return train, val Dataset split
Unpack mean, std = compute_stats(x) Normalization parameters
Star unpack first, *rest = epochs Separate warmup epoch
Swap a, b = b, a Reorder without temp variable
def min_max_scale(values): """Return (scaled_values, (min, max)) — stats frozen as tuple.""" vmin, vmax = min(values), max(values) scaled = [(v - vmin) / (vmax - vmin) for v in values] return scaled, (vmin, vmax) data = [10.0, 20.0, 30.0, 40.0] scaled, stats = min_max_scale(data) print(scaled) print(stats) # (10.0, 40.0) — reuse for inference
What’s Next in This ModuleThe final lecture, Set, covers unordered collections of unique elements—vocabulary tokens, label classes, and deduplicated user IDs.

When to Choose Tuple vs List

Scenario Prefer Reason
Growing loss history List Needs append
Fixed image shape Tuple Immutable contract
Dict key for cache Tuple Hashable
Batch of samples List Mutable, variable length

Common Misconceptions

Misconception 1: “Tuples are just lists that cannot grow.”

Why people believe it: Similar indexing and slicing syntax.

Reality: Immutability is semantic, not just operational. Tuples signal fixed structure; they are hashable when elements are hashable—lists are not.

Misconception 2: “Tuples containing lists are fully immutable.”

Why people believe it: The tuple object itself cannot be reassigned.

Reality: A tuple of lists is hashable only if all elements are hashable—lists inside tuples can still be mutated. For true immutability, use nested tuples or frozen data structures.

Misconception 3: “Parentheses are required for tuples.”

Why people believe it: Textbooks show (1, 2, 3).

Reality: Commas create tuples: a = 1, 2, 3. Parentheses disambiguate in expressions like (x + y,).

Quick Knowledge Check

  1. Short Answer: Main difference between list and tuple? Answer: Tuples are immutable; lists are mutable.
  2. True/False: Tuples can be dictionary keys if hashable. Answer: True.
  3. Multiple Choice: (42) type is: (a) tuple, (b) int, (c) list, (d) set. Answer: (b) — need (42,) for tuple.
  4. Short Answer: Write a tuple for RGB color (255, 128, 0). Answer: (255, 128, 0).
  5. True/False: shape[0] = 256 works on a tuple. Answer: False.
  6. Short Answer: What does a, b = fn() assume fn returns? Answer: An iterable of two values (often a tuple).
  7. Multiple Choice: Best type for fixed (height, width): (a) list, (b) tuple, (c) dict, (d) set. Answer: (b).
  8. True/False: Tuple slicing returns a new tuple. Answer: True.
  9. Short Answer: Why return normalization stats as a tuple? Answer: Immutable fixed pair for reuse at inference.
  10. Multiple Choice: Tuple with a list inside is hashable? Answer: No — list is unhashable.

Key Takeaways

  • Tuples are ordered, immutable sequences created with commas/parentheses.
  • Immutability enables hashable records usable as dictionary keys and cache indices.
  • Unpacking tuples cleanly binds multiple return values: scaled, stats = fn().
  • Use tuples for fixed schemas (shapes, stats pairs); lists for growing collections.
  • Tuples inside tuples are safer keys than tuples containing mutable lists.
  • Next: Set for unique-element collections.
Trainer’s Guide

Hands-on idea: Implement a shape-keyed cache dict using (batch, seq_len, dim) tuples as keys.

Debugging exercise: Show (42) vs (42,) with type().

Discussion prompt: Should train/val split indices be stored as lists or tuples?

What’s Next Continue to Set—the module capstone on unique elements and practical deduplication.