sifr.collections module provides the familiar Python collection types you already know — Counter, deque, defaultdict, plus set construction and operation helpers — with one important safety improvement: indexing operations that could fail return Option instead of raising KeyError or IndexError. You get compile-time visibility into every case where a lookup might not find a value.
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
Sifr represents sets as deduplicatedlist[T] values constructed through the helper functions in sifr.collections. This keeps ownership unambiguous while providing the set operations you need:
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.