Ordered set data structures and sequence utilities.

This module provides ordered set implementations that preserve insertion order while supporting the full Set protocol. Two variants are provided:

The dedup_list() helper deduplicates a sequence while preserving element order.

Module Contents

Classes

OrderedFrozenSet

An immutable ordered set that preserves…

OrderedSet

A mutable ordered set that preserves insertion…

Functions

dedup_list(iterable, lifo)

Return a list of distinct items from iterable.

dedup_list(iterable: collections.abc.Iterable[object], lifo: bool = False) list[object][source]

Return a list of distinct items from iterable.

By default first element will be kept at their index removing further duplicate occurrences. Setting lifo to True will inverse this behavior.

Parameters:
iterable: collections.abc.Iterable[object]

The input Sequence.

lifo : bool, optional

If True, return the distinct elements in Last-In-First-Out order. Defaults to False.

Returns:

list – A new list containing only distinct items.

class OrderedFrozenSet(iterable: collections.abc.Iterable[_V] | None = None)[source]

Extends: _AbstractOrderedSet[_V], collections.abc.Hashable

An immutable ordered set that preserves insertion order.

OrderedFrozenSet supports the full Set protocol plus the Sequence protocol (indexing, reversed(), index(), count()). Because it is Hashable it can be used as a dictionary key or as a member of another set.

For a mutable variant see OrderedSet.

Note

__getitem__ method uses a linear search through the keys, which is inefficient (O(n) complexity) compared to a frozenset.

inherited Members

copy()

Return a shallow copy of the set.

issubset(other)

Return whether every element in the set is in…

issuperset(other)

Return whether every element in other is in…

union(*others)

Return a new set with elements from the set…

intersection(*others)

Return a new set with elements common to the…

difference(*others)

Return a new set with elements in the set that…

symmetric_difference(*others)

Return a new set with elements in either the…

index(item)

Return the position of item in the set.

count(item)

Return 1 if item is in the set, 0

isdisjoint(other)

Return True if two sets have a null intersection.

class OrderedSet(iterable: collections.abc.Iterable[_V] | None = None)[source]

Extends: _AbstractOrderedSet[_V], collections.abc.MutableSet[_V]

A mutable ordered set that preserves insertion order.

OrderedSet extends MutableSet with insertion-order memory and supports the Sequence protocol (indexing, reversed(), index(), count()).

Note

The pop() method removes and returns the most recently added element (LIFO order).

Note

__getitem__ method uses a linear search through the keys, which is inefficient (O(n) complexity) compared to a set.

inherited Members

copy()

Return a shallow copy of the set.

issubset(other)

Return whether every element in the set is in…

issuperset(other)

Return whether every element in other is in…

union(*others)

Return a new set with elements from the set…

intersection(*others)

Return a new set with elements common to the…

difference(*others)

Return a new set with elements in the set that…

symmetric_difference(*others)

Return a new set with elements in either the…

index(item)

Return the position of item in the set.

count(item)

Return 1 if item is in the set, 0

isdisjoint(other)

Return True if two sets have a null intersection.

remove(value)

Remove an element. If not a member, raise a…

add(value: _V) None[source]

Add value to the set if it is not already present.

discard(value: _V) None[source]

Remove value from the set if it is present; otherwise do nothing.

pop() _V[source]

Return and remove the most recently added element (LIFO order).

Raises:

KeyError – If the set is empty.

clear() None[source]

Remove all elements from the set.

update(*others: collections.abc.Iterable[_V]) None[source]

Update the set, adding the union of all iterables.

intersection_update(*others: collections.abc.Iterable[_V]) None[source]

Update the set, keeping only elements found in it and all others.

difference_update(*others: collections.abc.Iterable[_V]) None[source]

Update the set, removing elements found in others.

symmetric_difference_update(*others: collections.abc.Iterable[_V]) None[source]

Update the set, keeping only elements found in either set, but not in both.