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:
OrderedFrozenSet— an immutable, hashable ordered set.OrderedSet— a mutable ordered set supporting in-place updates.
The dedup_list() helper deduplicates a sequence while preserving
element order.
Module Contents¶
An immutable ordered set that preserves… |
|
A mutable ordered set that preserves insertion… |
|
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.
-
class OrderedFrozenSet(iterable: collections.abc.Iterable[_V] | None =
None)[source]¶ Extends:
_AbstractOrderedSet[_V],collections.abc.HashableAn immutable ordered set that preserves insertion order.
OrderedFrozenSetsupports the fullSetprotocol plus theSequenceprotocol (indexing,reversed(),index(),count()). Because it isHashableit 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
1if 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.
OrderedSetextendsMutableSetwith insertion-order memory and supports theSequenceprotocol (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
1if 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…
- 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.