Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Tuple Packing: Syntax, Behavior, and Use Cases

python tuple packing: Learn how Python tuple packing works in assignments, function returns, and value swaps, with edge cases and runtime behavior explained.

~4 min readPython
#python#tuple#multiple assignment#unpacking
Illustration of Python tuple packing showing comma-separated values being collected into a tuple container.

In Python, a comma-separated expression list creates a tuple. When you write coordinates = 10, 20, the expression 10, 20 is evaluated as a tuple (10, 20) and assigned to coordinates. This implicit tuple creation is called tuple packing. No parentheses are required, although they are often added for readability.

python
point = 10, 20 print(point) # (10, 20) print(type(point)) # <class 'tuple'>

The assignment statement evaluates the right-hand side completely before binding it to the target. Because the comma-separated expression forms a tuple, the variable receives a tuple object.

How Assignment Uses Packed Tuples

The most common form of tuple packing appears in multiple assignment:

python
x, y = 1, 2

Here the right side 1, 2 is packed into a tuple (1, 2), and then the tuple is unpacked into x and y. The assignment statement performs both operations in sequence: it packs the right-hand side into a tuple, then unpacks that tuple into the left-hand targets.

This is why, without a starred target, the number of targets on the left must match the number of values on the right. If they do not match, Python raises a ValueError at runtime.

python
a, b = 1, 2, 3 # ValueError: too many values to unpack

Packing Versus Unpacking

Packing and unpacking are opposite operations. Packing collects values into a tuple; unpacking distributes a tuple's elements into separate variables.

python
# Packing pair = 1, 2 # pair is (1, 2) # Unpacking first, second = pair # first = 1, second = 2

The same comma syntax serves both purposes. On the right side of an assignment, commas pack. On the left side, commas define unpacking targets. Recognizing which role the comma plays in a given line is the key to reading this syntax correctly.

Returning Packed Tuples from Functions

A function can return multiple values by packing them into a tuple:

python
def divide_and_remainder(dividend, divisor): quotient = dividend // divisor remainder = dividend % divisor return quotient, remainder

The return statement packs quotient and remainder into a tuple. The caller can unpack the result directly:

python
q, r = divide_and_remainder(17, 5) print(q, r) # 3 2

This pattern is idiomatic Python. It avoids defining a small class or dataclass when the returned values are conceptually related but simple, and it keeps the call site readable. If the function grows to return more than three or four values, consider whether a named structure would make the contract clearer.

Swapping Values Without a Temporary Variable

Tuple packing and unpacking make value swapping concise:

python
a = 5 b = 10 a, b = b, a

The right-hand side b, a is packed into a tuple (10, 5) before any assignment occurs. Then the tuple is unpacked into a and b. Because the packing happens first, the original values are preserved during the swap, and no temporary variable is needed.

This swap works for any objects, not just numbers. Lists, dictionaries, and custom objects can be swapped the same way because the tuple holds references.

Edge Cases and Common Mistakes

The most common error with tuple packing is a mismatch between the number of packed values and the number of unpacking targets. Without a starred target, Python raises ValueError at runtime, not at parse time, so the failure appears only when the line executes.

python
x, y, z = 1, 2 # ValueError: not enough values to unpack

Another subtle case involves the starred expression, which allows partial unpacking:

python
first, *rest = 1, 2, 3, 4 print(first) # 1 print(rest) # [2, 3, 4]

The starred target collects the remaining values into a list, not a tuple. This assignment-target form works only in assignment contexts. In function parameter definitions, *args serves a different role and collects positional arguments into a tuple.

A related edge case is a single-element tuple. The expression 1, produces a one-element tuple, while (1) is just the integer 1. This distinction matters when packing a single value explicitly:

python
single = 1, print(single) # (1,)

Runtime Behavior and Memory Considerations

Tuple packing stores values by reference, not by copy. A tuple created from an expression is an object; once it is no longer referenced, it becomes eligible for reclamation. In CPython, simple cases such as constant tuple literals can be optimized and may not allocate a fresh tuple on every evaluation, so avoid relying on the exact allocation behavior of a particular interpreter version.

When you pack objects such as strings or lists, the tuple stores references to those objects, not copies. Mutating a list that was packed into a tuple is visible through the tuple:

python
items = [1, 2] data = items, "label" items.append(3) print(data[0]) # [1, 2, 3]

This reference behavior matters when packed tuples are stored in data structures and later inspected. If you need an immutable snapshot of a mutable object, pack a copy instead:

python
items = [1, 2] data = list(items), "label" items.append(3) print(data[0]) # [1, 2]

Choosing between packing references and copies depends on whether the caller expects the tuple to reflect later mutations. For configuration values that should stay fixed, copying the mutable component at packing time prevents surprising changes from propagating through the tuple.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.