Extra Python Collections - bags (multisets) and setlists (ordered sets)

2.0.2 · active · verified Sat Apr 11

collections_extended is a pure Python module with no dependencies providing various extended collection types. It includes a bag (multiset) class, a setlist (ordered set / unique list) class, a bijection, a RangeMap, and an IndexedDict. It also offers frozen (hashable) variants of bags and setlists. The current version, 2.0.2, maintains an active development status with regular updates and is compatible with Python 3.7+.

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `bag`, `setlist`, `IndexedDict`, and `RangeMap` from `collections-extended`.

from collections_extended import bag, setlist, IndexedDict, RangeMap
from datetime import date

# Example with bag (multiset)
b = bag('abracadabra')
print(f"Bag: {b}, Count of 'a': {b.count('a')}")
b.remove('a')
print(f"Bag after removing 'a': {b}, Count of 'a': {b.count('a')}")

# Example with setlist (ordered set)
sl = setlist('abracadabra')
print(f"Setlist: {sl}, element at index 3: {sl[3]}")
try:
    sl.insert(1, 'd')
except ValueError as e:
    print(f"Attempted to insert duplicate into setlist: {e}")

# Example with IndexedDict
idict = IndexedDict({'a': 1, 'b': 2, 'c': 3})
print(f"IndexedDict: {idict}, value at index 1: {idict.iloc[1]}")

# Example with RangeMap
version_map = RangeMap()
version_map[date(2017, 10, 20): date(2017, 10, 27)] = '0.10.1'
version_map[date(2017, 10, 27):] = '1.0.0'
print(f"Version for 2017-10-24: {version_map[date(2017, 10, 24)]}")

view raw JSON →