Collection protocols
These protocols describe the shared APIs behind built-in collection types.
These protocols describe behavior, not concrete storage. They let the language talk about "something indexable" or "something iterable" without naming one specific collection type.
Core collection protocols
Indexed[A(Eq), B]: lookup, assignment, and deletion with[]Sliceable[A] (Indexed[int, A]): slicing withstart:stop:stepCollection[A] (Iterable[A]): construction from an iterable andlenContainer[A(Eq)] (Collection[A]): membership withinandnot in
Specialized collection protocols
Sequence[A] (Sliceable[A], Collection[A], Times[int]): ordered, sliceable collections with repetition and mutating operations such asappend,insert, andreverseMapping[A(Eq), B] (Container[A], Indexed[A, B]): key/value collections withget,pop,keys,values, anditemsSet[A(Eq)] (Container[A], Ord, Logical, Minus): set operations, ordering, membership, and mutating updates such asaddanddiscard
Built-in types make use of these protocols:
list[A]implementsSequence[A]dict[A(Hashable), B]implementsMapping[A, B]set[A(Hashable)]implementsSet[A]
The protocol surface is sometimes looser than a concrete type's full
requirements. For example, Mapping[A, B] talks about
key/value behavior in general, while dict specifically
requires hashable keys.