ADR 0027: list[T], and why the language has two container types¶
Status: Final
Type: Feature
Created: 2026-07-30
Amends: 0004 (the literal changes owner)
Amended-By: 0024 (the element-kind flag became a character of the descriptor alphabet; instance elements and list-valued fields admitted), 0030 (the out-of-range trap became an IndexError raise), 0053 (the provably-dead check elided), 0068 (an element takes its declared type's width)
Related-ADRs: 0028 (what you can say about an aggregate), 0031 (the empty literal), 0057 (lists of lists). Provenance: arbitrations A64, A69–A72.
Context¶
The language already had a container before it had list: the fixed 1-D Array[T] the numeric path is built on. A growable list[T] either coexists with it or replaces it. This ADR decides that the language has both, meaning different things, then gives the literal to the one CPython says it belongs to.
Decision¶
Two types¶
Array[T] |
list[T] |
|
|---|---|---|
| what it is | a layout | an object |
| header | none | count, length, capacity, element kind |
| element width | T's own itemsize |
its declared type's width: one byte for a bool, eight for an int, a float or a reference (0068) |
| growth | never | amortised O(1); append may move the storage |
| owns its elements | no | yes; released when the list dies |
| reclaimed | no (leak-at-exit) | yes, by the heap memory model |
| a NumPy ufunc can receive it | yes, via the buffer protocol | no |
len |
folds to a constant for a literal | a runtime field read |
They cannot be one type. A refcount header in front of a buffer NumPy is reading is a header NumPy would have to know about; a capacity a realloc can move is a data pointer going stale under a descriptor a caller already holds. Making list growable-and-buffer-compatible would break one side or the other. Array[T] is the numeric type (fixed shape, what a kernel needs); list[T] is the container type (what ordinary code reaches for).
The literal is a list¶
The literal [a, b, c] lowers as a list unless the binding is annotated Array[T] (or the name is already bound to one), in which case it is that array's initialiser. The default was originally the other way round, and flipping it is not a convenience: with the array as the default, the literal silently means the container CPython does not; an ordinary Python program would meet the divergence at the first append. The numeric path pays nothing: a program that wants a buffer already says so in the annotations. The allocators have separate names, because types sharing one symbol are a defect waiting for a reader.
Semantics that must not be optimised¶
lenof a list must not fold.[1]then.append(2)has length 2; a compile-time 1 is silently wrong. (An array literal's length is in the source and folds.)- The iteration bound is re-read on each pass. This is CPython's list-iterator rule: a body that shortens the list stops early, a body that appends does not terminate. A hoisted bound would read past the end of a shrunk list, a divergence in the direction of a memory error. (0053 elides checks only where the loop's own shape proves them dead.)
- An out-of-range index raises in both build modes. A fixed array's shape is static, so its release build can elide the check with something standing behind it; a list's length is not knowable at compile time, so an unchecked release read is indeterminate memory, silently. That is the one direction the ownership discipline must never fail. The raise is CPython's
IndexError(0030).
Representation choices¶
- The accessors are per-element-type triples: one C type per IR type is what lets the ABI check say a
Strelement read through the integer accessor is wrong, which avoid *accessor makes invisible. - A list's C type names itself (
__pp_list *, incomplete outside the runtime), nevervoid *, so the checker can tell a list from a channel from an instance. - The element kind is a property of the container. Homogeneity pays twice: the release loop asks the question once for a million elements. No arity limit applies (a per-slot mask caps at the word width). The kind is one character of the descriptor alphabet (0024).
- A list element store releases what it replaces. This is possible because every slot below
lenwas written by an append; an array cannot (unwritten elements are indeterminate, so "holds a Str" and "never written" are indistinguishable), which is why array element stores never release.
The methods¶
Every method CPython's list defines lowers, as CPython defines it, with the IndexError and ValueError texts CPython gives, checked in generated code so they show the author's line:
append,extend,insert,pop,remove,index,count,clear,reverse,sortandcopy.poptakes its own reference to the element before the delete releases the list's, so what it returns is the caller's.insertis an append, then a move to the clamped index.remove,indexandcountscan for equal elements; the range ofindex(v, start, end)clamps as a slice's does.extendreads the source's length once, soxs.extend(xs)doubles the list, and walks any other iterable into a list first.copyis the full slice. An unknown name gets CPython's own error: the list has no such method.sortandsortedshare a stable merge sort, becauseqsort's premise, that equal elements are indistinguishable, fails for0.0and-0.0.reverse=Trueis CPython's own method: reverse, sort, reverse.key=is 0028's.xs + ysandxs * nreturn a fresh list holding a reference to each element they copy, so[[0] * 3] * 3holds one row three times, as CPython's does; a negative count is empty.xs += vislist.__iadd__, which isxs.extend(v)returningxs: it changes the list every other name for it sees, accepts any iterable, andxs += xsdoubles it. It lowers as that call over the target read once (a name, a field or an element of a list of lists). Lowering it asxs = xs + vwould hide the change from the other names.xs[lo:hi] = ys(__pp_list_splice): the bounds clamp as a slice's do, the range is replaced byys's elements and the list grows or shrinks; the new elements' references are taken before the old ones are released, soxs[1:3] = xsis safe. An assignment to an extended slice (with a step) lowers too.==and<on whole lists compare in CPython's lexicographic order: the first element that differs decides, then the shorter list comes first, through a runtime helper that returns -1, 0 or 1. Any list compares with[]by its length. A float list is refused, because CPython tries identity before==, and a NaN element answers differently (the caveatinandcounthave, A192).
The task boundary¶
A list may not cross a task boundary. The message states both reasons: the sendability rule classes it as uniquely-owned-mutable, crossing by move; the compiler has no move tracking. Copy-on-send, built for Str, is not taken: the retention counters are per task, so a crossing list would put the allocation in one task's ledger and its release in another's, which no per-task counter can balance. Spending the instrument to buy the feature is the wrong trade while the instrument is the only thing watching the feature.
Consequences¶
- 200,000 strings appended and walked release through the container that owns them, identically across the backends. The container is the third type under one slot rule, added as one row in the counted-type table.
try: xs[i] except IndexError:is idiomatic Python that works (0030); the decided refusals (crossing tasks, a float list compared) state their reason.
Alternatives considered¶
- One container type: rejected above; the buffer contract and the ownership contract are incompatible in one object.
- Keeping the literal on the array: rejected: the literal must mean what CPython means, or ordinary programs diverge at first growth.
- A summed retention counter for containers and elements: rejected: a leaked container must be distinguishable from a leaked element, so the cycle-report trigger has to name which count is live.