Indexable Set Recipe

class sortedcollections.IndexableSet(*args, **kwargs)[source]

Set that supports numerical indexing.

Values are numerically indexable. For example:

>>> indexable_set = IndexableSet('abcde')
>>> sorted(indexable_set[:]) == ['a', 'b', 'c', 'd', 'e']

IndexableSet implements the sequence abstract base class.

__init__(*args, **kwargs)[source]

Initialize sorted set instance.

Optional iterable argument provides an initial iterable of values to initialize the sorted set.

Optional key argument defines a callable that, like the key argument to Python’s sorted function, extracts a comparison key from each value. The default, none, compares values directly.

Runtime complexity: O(n*log(n))

>>> ss = SortedSet([3, 1, 2, 5, 4])
>>> ss
SortedSet([1, 2, 3, 4, 5])
>>> from operator import neg
>>> ss = SortedSet([3, 1, 2, 5, 4], neg)
>>> ss
SortedSet([5, 4, 3, 2, 1], key=<built-in function neg>)
  • iterable – initial values (optional)

  • key – function used to extract comparison key (optional)


Support for pickle.

The tricks played with exposing methods in SortedSet.__init__() confuse pickle so customize the reducer.