Featurecore
Expression lists
Also called Python comma operator, tuple packing, trailing comma, comma-separated expressions, iterable unpacking in expressions.
An expression list is expressions separated by commas. Outside a list or set display it yields a tuple: the comma makes the tuple, not the parentheses, and a starred item unpacks an iterable into it.
- Language
- Python
Synopsis
point = 3, 4
single = 5,
plain = (5)
empty = ()
print(point, single, plain, empty)
print(type(single).__name__, type(plain).__name__)
(3, 4) (5,) 5 ()
tuple int
Semantics
r1. "Except when part of a list or set display, an expression list containing at least one comma yields a tuple." The tuple has one item per expression, and "The expressions are evaluated from left to right."1 Example: left-to-right-list.
r2. "A trailing comma is required only to create a one-item tuple", and it is optional elsewhere. A single expression without a trailing comma is just that expression's value, and the empty tuple is written ().1 Example: tuple-by-comma.
r3. "Note that it is actually the comma which makes a tuple, not the parentheses." The parentheses are optional except for the empty tuple or to avoid ambiguity.2 Example: paren-not-tuple.
r4. An item of an expression list, or of a tuple, list or set display, may be prefixed with *. The starred expression must evaluate to an iterable, whose items are spliced into the new tuple, list or set at that point.3 Example: unpack-items.
r5. Writing several values separated by commas is called tuple packing, and the reverse, x, y, z = t, is sequence unpacking.4 So return a, b returns one tuple, which the caller can unpack. Example: return-tuple.
Examples
The items are evaluated in order (rule r1).
order = []
def mark(n):
order.append(n)
return n
values = mark(1), mark(2), mark(3)
print(values, order)
(1, 2, 3) [1, 2, 3]
Starred items in expression lists and displays (rule r4).
head = [1, 2]
tail = (3, 4)
combined = *head, *tail, 5
print(combined)
print([*head, *tail], {*head, *head})
(1, 2, 3, 4, 5)
[1, 2, 3, 4] {1, 2}
Packing on return, unpacking at the call site (rule r5).
def min_max(values):
return min(values), max(values)
result = min_max([4, 1, 9])
low, high = result
print(result, low, high)
(1, 9) 1 9
Pitfalls
A stray comma makes a tuple
A trailing comma at the end of an assignment turns the value into a one-item tuple (rule r2).
This code runs but gives a wrong answer on purpose.
total = 10,
print(total, total == 10)
(10,) False
Parentheses alone do not make a tuple
("solo") is a string, so the loop walks its characters (rule r3). Write ("solo",).
This code runs but gives a wrong answer on purpose.
for item in ("solo"):
print(item)
s
o
l
o
A lone starred expression
A starred item needs a list to be spliced into; on its own it is a syntax error (rule r4). Add a comma: x = *items,.
This code raises SyntaxError, on purpose.
items = [1, 2]
x = *items
Comparison
Go has no tuple type. Its assignment statement can assign "multiple values to a matching number of variables", and a tuple assignment takes the values of a multi-valued operation such as a function call.5 A Python function that returns a, b returns one tuple object (rule r5), which can be stored, passed around or unpacked later.
References
- The Python Language Reference, Python 3.15, 6.15 Expression lists
- Built-in Types, Python 3.15 documentation, class tuple
- The Python Language Reference, Python 3.15, 6.15.1 Iterable unpacking
- The Python Tutorial, Python 3.15, 5.3 Tuples and Sequences
- The Go Programming Language Specification, Assignment statements
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag F7535S Edit this page History Report an error Markdown