CKTIS / Python
Unpacking targets and call arguments
Assignment unpacking binds items from an iterable to targets, while call unpacking supplies separate arguments to a function.
Version: Python 3.14; repository image, production runtime not attested
Syntax
a, b = pair
first, *middle, last = values
for a, b in pairs: ...
f(*iterable, **mapping)Assignment unpacking binds items from an iterable to targets, while call unpacking supplies separate arguments to a function.
An unstarred target list must receive exactly one item per target or raises ValueError. One starred target collects the remaining items into a list. A loop applies the same assignment rule to each incoming pair before executing the loop body.
In a call, * expands an iterable into positional arguments and ** expands a mapping into keyword arguments whose keys must be strings. A parameter supplied both positionally and by keyword raises TypeError. The call binds parameters of the function; it does not assign target names in the calling scope.
Examples
pairs = [("oak", 2), ("elm", 5)]
for name, count in pairs:
print(name, count)
first, *middle, last = range(5)
print(first, middle, last)
def label(name, count, *, unit):
return f"{name}/{count}/{unit}"
print(label(*pairs[0], **{"unit": "kg"}))
try:
label(*pairs[0], **{"name": "pine", "unit": "kg"})
except TypeError as error:
print(type(error).__name__)
Result
oak 2 elm 5 0 [1, 2, 3] 4 oak/2/kg TypeError
In a call, * expands an iterable into positional arguments and ** expands a mapping into keyword arguments whose keys must be strings. A parameter supplied both positionally and by keyword raises TypeError. The call binds parameters of the function; it does not assign target names in the calling scope.