Featurecore
Container displays
Also called display, list literal, set literal, dict literal, tuple literal, trailing comma, empty set, star unpacking in a display, double star in a dict display.
A display builds a list, set, tuple or dict from items in brackets, braces or parentheses. Items are evaluated left to right, a trailing comma is allowed, and * or ** unpacks an iterable or mapping.
- Language
- Python
Synopsis
print(["one", "two", 1 + 2])
print(sorted({"b", "a", "b"}))
print((1, "x"), ("single",))
print({1: "one", 2: "two"})
['one', 'two', 3]
['a', 'b']
(1, 'x') ('single',)
{1: 'one', 2: 'two'}
Square brackets make a list, braces make a set or a dict, and parentheses with a comma make a tuple.
Semantics
r1. "For constructing builtin containers (lists, sets, tuples or dictionaries), Python provides special syntax called displays."1 "All displays, however, consist of comma-separated items enclosed in paired delimiters."1
r2. "In list, tuple and dictionary (but not set) displays, the series may be empty".1 "There is no special syntax for the empty set. The {} literal is a dictionary display that constructs an empty dictionary. Call set() with no arguments to get an empty set."1 Example: empty.
r3. "If the series is not empty, the items may be followed by an additional comma, which has no effect".1 Example: trailing.
r4. "To avoid ambiguity, if a tuple display has exactly one element, it requires a trailing comma. Without it, you get a parenthesized group".1 Example: single.
r5. "At runtime, when a display is evaluated, the listed items are evaluated from left to right and placed into a new container of the appropriate type."1
r6. "For tuple, list and set (but not dict) displays, any item in the display may be prefixed with an asterisk (*). This denotes iterable unpacking."1 In a dict display, ** denotes dictionary unpacking, and "later values replace values already set by earlier items and unpackings."1 Example: unpack.
r7. In a dict display, "you can specify the same key multiple times in the comprehension, and the final dictionary’s value for a given key will be the last one given."1 Example: duplicate-key.
r8. PEP 448 "proposes extended usages of the * iterable unpacking operator and ** dictionary unpacking operators to allow unpacking in more positions, an arbitrary number of times, and in function calls and displays."2
Examples
print([], (), {}, set())
print(type({}).__name__, type(set()).__name__)
[] () {} set()
dict set
colors = [
"red",
"green",
"blue",
]
print(colors)
['red', 'green', 'blue']
A trailing comma lets a later edit add a line without a change to the line before it (rule r3).
numbers = (1, 2)
print([*numbers, "word", *numbers])
print({*"aba", "c"} == {"a", "b", "c"})
defaults = {"color": "blue", "count": 8}
overrides = {"color": "yellow"}
print({**defaults, **overrides})
[1, 2, 'word', 1, 2]
True
{'color': 'yellow', 'count': 8}
The value from overrides replaces the default value (rule r6).
print({1: "this will be overridden", 2: "two", 1: "one"})
{1: 'one', 2: 'two'}
Pitfalls
Parentheses without a comma do not make a tuple:
This code runs but gives a wrong answer on purpose.
point = ("single")
print(point, len(point))
single 6
("single") is a string in a group, and its length is 6. Write ("single",) for a one-item tuple (rule r4).
A single * in braces makes a set, not a dict:
This code raises TypeError, on purpose.
pairs = [("a", 1)]
d = {*pairs}
print(d["a"])
{*pairs} is a set display (rule r6), and a set has no indexing. Use dict(pairs) to make a dict from pairs.
Comparison
Go uses composite literals: "Composite literals construct new values for structs, arrays, slices, and maps each time they are evaluated."3 A Go literal names its type, for example []int{1, 2} or map[string]int{"a": 1}. A Python display gets its type from the delimiters, and a set or tuple display has no direct Go form.
References
- The Python Language Reference, Python 3.15, 6.2.4. Parenthesized groups; 6.2.5. Container displays
- PEP 448: Additional Unpacking Generalizations, Abstract
- The Go Programming Language Specification, Composite literals
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 ET0442 Edit this page History Report an error Markdown