Skip to main content
Sifr’s sifr.collections module provides familiar collection types such as Counter, deque, defaultdict, and frozenset. Built-in set[T] is a first-class language type and needs no import. Indexing operations that could fail return Option instead of raising KeyError or IndexError, so every missing-value case is visible in the type system.

Importing

Do not use bare import collections — that triggers SIFR-IMPORT-0008. Always use from sifr.collections import ....

Lists

Standard list[T] is a first-class language type in Sifr. You do not need to import it. Lists are mutable, ordered, and support all comprehension forms:
Appending to a list requires the mut qualifier on the binding that owns it:
For the core language model behind lists, dictionaries, optional values, and iteration, see Values and Collections and Iteration.

Dicts and Tuples

dict[K, V] and tuple[T, ...] are built-in types. Dict literals and comprehensions work exactly as in Python:
Safe access via .get(key) returns Option[V] (either the value or None). Direct indexing uses the same panic-free contract: scores["missing"] returns None instead of raising KeyError.

Sets

set[T] is a built-in generic mutable collection. Construct it directly and use its methods:

Counter

Counter[T] counts occurrences of hashable values. Build one from a list with from_list, then query counts or pull the most-common elements:
counts.get(key) returns the integer count for that key (0 if unseen). most_common(n) returns the top-n (value, count) pairs in descending order, exactly as Python’s Counter.most_common does.

deque

deque[T] is a double-ended queue. Pass maxlen to create a bounded deque that automatically drops the oldest element when capacity is exceeded — useful for sliding windows and ring buffers:
pop() on an empty deque returns None rather than raising IndexError. Check the return value before using it:

Full Demo

The following is the complete collections demo from the Sifr repository, showing sets, Counter, and deque working together:

JSON and Serialization

For working with JSON data structures, use sifr.json alongside your collections:
loads raises JSONDecodeError on malformed input. Use a try/except block whenever parsing untrusted data.