Skip to main content
Sifr’s 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

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

Sifr represents sets as deduplicated list[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:
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.