collections — 0.1.0

org.pycforge.collections · Author: PyCForge

License: GPL-3.0-only · Created: 2026-11-22

Runtime conversion · Direct declared calls

Using this import · Functions · Conversion contract · Worked examples

Function index

Counter · defaultdict · deque

Overview

Maintain a bounded history, count observations and collect values by key.

Using this import

Use *import collections as c. A missing Counter subscription returns zero. A missing defaultdict subscription creates an entry when a factory is set. The get method leaves both mappings unchanged.

Author's limitations

Runtime conversion. deque supports both ends, optional maxlen, extension, rotation, reversal, copying, indexing and iteration. Counter accepts Runtime hashable keys and signed 64-bit integer counts. defaultdict accepts None, list and int factories. Constructors accept ordinary arguments and named entries. Expanded calls, Typed conversion and other collections exports are outside this profile.

Release notes

Initial Runtime collections constructors.

Conversion contract

Use *import collections in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.

Runtime conversion. deque supports both ends, optional maxlen, extension, rotation, reversal, copying, indexing and iteration. Counter accepts Runtime hashable keys and signed 64-bit integer counts. defaultdict accepts None, list and int factories. Constructors accept ordinary arguments and named entries. Expanded calls, Typed conversion and other collections exports are outside this profile.

collections.Counter(iterable=None, /, **entries) -> counter-value

Count hashable items in encounter order, or initialize counts from a mapping.

Normalized semantic slots: iterable, entries. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Count an iterable or supply integer counts as a mapping or named entries. Use update, subtract, total, elements and most_common(n=None). Ties keep encounter order. Missing subscriptions return zero without insertion. Stored zero and negative counts appear in items and are skipped by elements. Copy, get and mapping views are available.

Argument 1: iterable

Argument 2: entries

Returns: counter-value

View example: collection-operations

collections.defaultdict(default_factory=None, initial=(), /, **entries) -> defaultdict-value

Create an ordered mapping that initializes missing keys with the selected factory.

Normalized semantic slots: default_factory, initial, entries. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Use list, int or None for default_factory. On a missing subscript, list creates a fresh list and int supplies zero. None raises KeyError. Membership and get preserve the keys. Copy preserves the factory and shares existing values. Reassign default_factory to change future defaults.

Argument 1: default_factory

Argument 2: initial

Argument 3: entries

Returns: defaultdict-value

View example: collection-operations

collections.deque(iterable=(), maxlen=None) -> deque-value

Build a double-ended queue with an optional maximum length.

Normalized semantic slots: iterable, maxlen. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Use append, appendleft, pop, popleft, extend, extendleft, rotate, reverse, clear, copy, count, index, remove and insert. maxlen is read-only. A full bounded queue evicts from the opposite end on append. Empty pops and full insertions raise IndexError. Use len, bool, membership, equality, integer indices, assignment, deletion, iter and reversed.

Argument 1: iterable

Argument 2: maxlen

Returns: deque-value

View example: collection-operations

Worked examples

Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.

Example: collection-operations

*import collections as c
def main():
    recent = c.deque([1, 2], maxlen=2)
    recent.append(3)
    print(list(recent))
    counts = c.Counter("abac")
    print(counts.most_common(), counts["absent"])
    groups = c.defaultdict(list)
    groups["odd"].append(3)
    print(groups["odd"], groups.get("even"))
    return 0

Expected standard output:

[2, 3]
[('a', 2), ('b', 1), ('c', 1)] 0
[3] None

Expected exit code: 0