OrderedSet

class orderedsets.OrderedSet[source]

A set class that preserves insertion order.

It implements exactly the same API as set and can be used as a drop-in replacement for that class when ordering is desired.

>>> oset = OrderedSet(["a", "b", "c", "d"])
>>> oset.add("X")
>>> oset
OrderedSet({'a', 'b', 'c', 'd', 'X'})
>>> oset == set(["a", "b", "c", "d", "X"])
True

FrozenOrderedSet

class orderedsets.FrozenOrderedSet[source]

A frozen set class that preserves insertion order.

It implements exactly the same API as frozenset and can be used as a drop-in replacement for that class when ordering is desired.

>>> foset = FrozenOrderedSet(["a", "b", "c", "d"])
>>> foset
FrozenOrderedSet({'a', 'b', 'c', 'd'})
>>> foset == set(["a", "b", "c", "d"])
True

IndexSet

class orderedsets.IndexSet[source]

A set class that preserves insertion order and allows indexing.

The only change in API from set is the addition of the __getitem__() method.

__getitem__(index: int | slice) T | list[T][source]

Return the element at index or a list of elements for a slice.

>>> iset = IndexSet(["a", "b", "c", "d", "e", "f", "g", "h", "i"])
>>> iset[0]
'a'
>>> iset[-1]
'i'
>>> iset[-2:-8:-2]
['h', 'f', 'd']

FrozenIndexSet

class orderedsets.FrozenIndexSet[source]

A frozen set class that preserves insertion order and allows indexing.

The only change in API from frozenset is the addition of the __getitem__() method.

__getitem__(index: int | slice) T_cov | list[T_cov][source]

Return the element at index or a list of elements for a slice.

>>> fiset = FrozenIndexSet(["a", "b", "c", "d", "e", "f", "g", "h", "i"])
>>> fiset[0]
'a'
>>> fiset[-1]
'i'
>>> fiset[-2:-8:-2]
['h', 'f', 'd']

Type Variables

class orderedsets.T

A type variable for items in an OrderedSet and IndexSet. All items must be hashable.

class orderedsets.T_cov

A (covariant) type variable for items in a FrozenOrderedSet and FrozenIndexSet. All items must be hashable.