Back to Study Material Python

Tuple

A tuple is an immutable, ordered sequence of items. Create one with parentheses: coords = (3, 4) or without: coords = 3, 4 (the comma creates the tuple, no...

Tuple

What is a Tuple?

A tuple is an immutable, ordered sequence of items. Create one with parentheses: coords = (3, 4) or without: coords = 3, 4 (the comma creates the tuple, not the parentheses). A single-element tuple requires a trailing comma: single = (42,) — without it, (42) is just the integer 42 in parentheses. Tuples support all the same indexing, slicing, and searching operations as lists, but their contents cannot be changed after creation.

Tuple Unpacking

Tuple unpacking assigns each element to a separate variable: x, y = (3, 4). This works with any iterable on the right side. Functions often return multiple values as a tuple: def min_max(lst): return min(lst), max(lst); lo, hi = min_max([3,1,4,1,5]). Extended unpacking: first, *rest = (1, 2, 3, 4) gives first=1 and rest=[2,3,4]. Swapping variables is clean with tuple unpacking: a, b = b, a works because the right side creates a temporary tuple.

Tuples as Dictionary Keys

Because tuples are immutable and hashable, they can be used as dictionary keys and set elements. Lists cannot serve as keys because they are mutable. This makes tuples ideal for composite keys: grid = {}; grid[(0, 0)] = "start"; grid[(1, 2)] = "treasure". This pattern is heavily used in dynamic programming, graph algorithms, and caching 2D or multi-dimensional state. Attempting to use a list as a dict key raises TypeError: unhashable type: 'list'.

Named Tuples

The collections.namedtuple factory creates tuple subclasses with named fields: from collections import namedtuple; Point = namedtuple("Point", ["x", "y"]); p = Point(3, 4); p.x gives 3; p.y gives 4. Named tuples are still immutable and work like regular tuples, but fields are accessible by name, not just index, dramatically improving readability. The dataclasses module (Python 3.7+) provides a more powerful alternative for complex cases.

Tuple vs List: When to Use Each

Use a tuple when the collection's size and content represent a fixed structure that should not change — like a (latitude, longitude) coordinate pair, an RGB colour (r, g, b), or a database row. Use a list when the collection is a sequence of similar items that may grow, shrink, or be modified — like a shopping cart, log entries, or a list of names. Tuples are also slightly more memory-efficient and faster to iterate than lists because Python can optimise their fixed structure.

Exam Tip

Common tuple exam questions: what is the difference between a tuple and a list? The key answer is immutability — tuples cannot be changed; lists can. Why is (42) not a tuple? Because without a trailing comma, Python treats it as 42 in parentheses. What is the type of (42,)? tuple. Can a tuple contain mutable objects? Yes — t = ([1,2], [3,4]) is valid, and you can modify the lists inside it. The tuple itself cannot be changed (you cannot replace its elements), but the contents of mutable items it holds can be changed.