Orderly Set
Orderly Set is a Python package providing several implementations of ordered set data structures, including `OrderlySet`, `StableSet`, `StableSetEq`, `OrderedSet`, and `RoughMaxSizeSet`. These implementations combine the uniqueness of a set with the order-preserving characteristics of a list or sequence, offering various performance trade-offs for operations such as insertion, deletion, membership testing, and index lookups. The library is actively maintained, with its current version being 5.5.0, and receives regular updates as evidenced by its GitHub release history.
Warnings
- breaking Version 5.4.0 changed behavior for accessing multiple items, switching from a singular `index` method to `indexes` for iterable input. Code using `index` for multiple items may break.
- gotcha There are several 'ordered set' libraries on PyPI (e.g., `orderedset` by simonpercivall, `ordered-set` by rspeer). Ensure you are importing from `orderly_set` as `from orderly_set import ...` to use this specific library.
- gotcha The `OrderedSet` class within `orderly-set` has O(N) deletion performance, which can be a bottleneck for applications with frequent deletions on large sets.
- gotcha The `StableSet` class within `orderly-set` has O(N) index lookup performance.
Install
-
pip install orderly-set
Imports
- OrderedSet
from orderly_set import OrderedSet
- StableSet
from orderly_set import StableSet
- RoughMaxSizeSet
from orderly_set import RoughMaxSizeSet
- OrderlySet
from orderly_set import OrderlySet
Quickstart
from orderly_set import OrderedSet, StableSet
# Using OrderedSet
letters = OrderedSet('abracadabra')
print(f"OrderedSet: {letters}") # Expected: OrderedSet(['a', 'b', 'r', 'c', 'd'])
print(f"'r' in letters: {'r' in letters}")
print(f"Index of 'r': {letters.index('r')}")
print(f"Item at index 2: {letters[2]}")
letters.add('x')
print(f"After adding 'x': {letters}")
# Using StableSet (different performance characteristics)
stable_items = StableSet([1, 5, 2, 5, 3])
print(f"StableSet: {stable_items}") # Expected: StableSet({1, 5, 2, 3})
stable_items.add(4)
print(f"After adding 4: {stable_items}")