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
Standardlist[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:
mut qualifier on the binding that owns it:
Dicts and Tuples
dict[K, V] and tuple[T, ...] are built-in types. Dict literals and comprehensions work exactly as in Python:
.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:
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, usesifr.json alongside your collections:
loads raises JSONDecodeError on malformed input. Use a try/except block whenever parsing untrusted data.