Welcome to orderedsets’ documentation¶

This site covers orderedsets’ API documentation. For more information about orderedsets, see the Github repository.

Usage¶

The OrderedSet class can be used as a drop-in replacement for set. To replace a frozenset, you can use a FrozenOrderedSet. The API is the same as OrderedSet, but without methods that mutate the set.

from orderedsets import FrozenOrderedSet, OrderedSet

os = OrderedSet([1, 2, 4])
os.add(0)
assert list(os) == [1, 2, 4, 0]
os.remove(0)

fos = FrozenOrderedSet([1, 2, 4])
# a.add(0)  # raises AttributeError: 'FrozenOrderedSet' object has no attribute 'add'
assert list(fos) == [1, 2, 4]

# Sets with the same elements compare equal
assert os == fos == {1, 2, 4} == frozenset([1, 2, 4])

# Only immutable sets can be hashed (and must have the same hash value if they
# compare equal)
assert hash(fos) == hash(frozenset([1, 2, 4]))

Some additional methods are provided, see the reference below.

API reference¶