class documentation

An OrderedSet is a custom MutableSet that remembers its order, so that every entry has an index that can be looked up. Example: >>> OrderedSet([1, 1, 2, 3, 2]) OrderedSet([1, 2, 3])

Method __and__ Undocumented
Method __contains__ Test if the item is in this ordered set
Method __eq__ Returns true if the containers have the same items. If `other` is a Sequence, then order is checked, otherwise it is ignored.
Method __getitem__ Get the item at a given index.
Method __getstate__ Undocumented
Method __init__ Undocumented
Method __iter__ Example: >>> list(iter(OrderedSet([1, 2, 3]))) [1, 2, 3]
Method __len__ Returns the number of unique elements in the ordered set
Method __repr__ Undocumented
Method __reversed__ Example: >>> list(reversed(OrderedSet([1, 2, 3]))) [3, 2, 1]
Method __setstate__ Undocumented
Method add Add `key` as an item to this OrderedSet, then return its index.
Method clear Remove all items from this OrderedSet.
Method copy Return a shallow copy of this object.
Method difference Returns all elements that are in this set but not the others.
Method difference_update Update this OrderedSet to remove items from one or more other sets.
Method discard Remove an element. Do not raise an exception if absent.
Method index Get the index of a given entry, raising an IndexError if it's not present.
Method intersection Returns elements in common between all sets. Order is defined only by the first set.
Method intersection_update Update this OrderedSet to keep only items in another set, preserving their order in this set.
Method issubset Report whether another set contains this set.
Method issuperset Report whether this set contains another set.
Method pop Remove and return the last element from the set.
Method symmetric_difference Return the symmetric difference of two OrderedSets as a new set. That is, the new set will contain all elements that are in exactly one of the sets.
Method symmetric_difference_update Update this OrderedSet to remove items from another set, then add items from the other set that were not present in this set.
Method union Combines all unique items. Each items order is defined by its first appearance.
Method update Update the set with the given iterable sequence, then return the index of the last element inserted.
Instance Variable items Undocumented
Instance Variable map Undocumented
Method _update_items Replace the 'items' list of this OrderedSet with a new one, updating self.map accordingly.
def __and__(self, other): (source)

Undocumented

def __contains__(self, key): (source)

Test if the item is in this ordered set Example: >>> 1 in OrderedSet([1, 3, 2]) True >>> 5 in OrderedSet([1, 3, 2]) False

def __eq__(self, other): (source)

Returns true if the containers have the same items. If `other` is a Sequence, then order is checked, otherwise it is ignored. Example: >>> oset = OrderedSet([1, 3, 2]) >>> oset == [1, 3, 2] True >>> oset == [1, 2, 3] False >>> oset == [2, 3] False >>> oset == OrderedSet([3, 2, 1]) False

def __getitem__(self, index): (source)

Get the item at a given index. If `index` is a slice, you will get back that slice of items, as a new OrderedSet. If `index` is a list or a similar iterable, you'll get a list of items corresponding to those indices. This is similar to NumPy's "fancy indexing". The result is not an OrderedSet because you may ask for duplicate indices, and the number of elements returned should be the number of elements asked for. Example: >>> oset = OrderedSet([1, 2, 3]) >>> oset[1] 2

def __getstate__(self): (source)

Undocumented

def __init__(self, iterable=None): (source)

Undocumented

def __iter__(self): (source)

Example: >>> list(iter(OrderedSet([1, 2, 3]))) [1, 2, 3]

def __len__(self): (source)

Returns the number of unique elements in the ordered set Example: >>> len(OrderedSet([])) 0 >>> len(OrderedSet([1, 2])) 2

def __repr__(self): (source)

Undocumented

def __reversed__(self): (source)

Example: >>> list(reversed(OrderedSet([1, 2, 3]))) [3, 2, 1]

def __setstate__(self, state): (source)

Undocumented

def add(self, key): (source)

Add `key` as an item to this OrderedSet, then return its index. If `key` is already in the OrderedSet, return the index it already had. Example: >>> oset = OrderedSet() >>> oset.append(3) 0 >>> print(oset) OrderedSet([3])

def clear(self): (source)

Remove all items from this OrderedSet.

def copy(self): (source)

Return a shallow copy of this object. Example: >>> this = OrderedSet([1, 2, 3]) >>> other = this.copy() >>> this == other True >>> this is other False

def difference(self, *sets): (source)

Returns all elements that are in this set but not the others. Example: >>> OrderedSet([1, 2, 3]).difference(OrderedSet([2])) OrderedSet([1, 3]) >>> OrderedSet([1, 2, 3]).difference(OrderedSet([2]), OrderedSet([3])) OrderedSet([1]) >>> OrderedSet([1, 2, 3]) - OrderedSet([2]) OrderedSet([1, 3]) >>> OrderedSet([1, 2, 3]).difference() OrderedSet([1, 2, 3])

def difference_update(self, *sets): (source)

Update this OrderedSet to remove items from one or more other sets. Example: >>> this = OrderedSet([1, 2, 3]) >>> this.difference_update(OrderedSet([2, 4])) >>> print(this) OrderedSet([1, 3]) >>> this = OrderedSet([1, 2, 3, 4, 5]) >>> this.difference_update(OrderedSet([2, 4]), OrderedSet([1, 4, 6])) >>> print(this) OrderedSet([3, 5])

def discard(self, key): (source)

Remove an element. Do not raise an exception if absent. The MutableSet mixin uses this to implement the .remove() method, which *does* raise an error when asked to remove a non-existent item. Example: >>> oset = OrderedSet([1, 2, 3]) >>> oset.discard(2) >>> print(oset) OrderedSet([1, 3]) >>> oset.discard(2) >>> print(oset) OrderedSet([1, 3])

def index(self, key): (source)

Get the index of a given entry, raising an IndexError if it's not present. `key` can be an iterable of entries that is not a string, in which case this returns a list of indices. Example: >>> oset = OrderedSet([1, 2, 3]) >>> oset.index(2) 1

def intersection(self, *sets): (source)

Returns elements in common between all sets. Order is defined only by the first set. Example: >>> oset = OrderedSet.intersection(OrderedSet([0, 1, 2, 3]), [1, 2, 3]) >>> print(oset) OrderedSet([1, 2, 3]) >>> oset.intersection([2, 4, 5], [1, 2, 3, 4]) OrderedSet([2]) >>> oset.intersection() OrderedSet([1, 2, 3])

def intersection_update(self, other): (source)

Update this OrderedSet to keep only items in another set, preserving their order in this set. Example: >>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.intersection_update(other) >>> print(this) OrderedSet([1, 3, 7])

def issubset(self, other): (source)

Report whether another set contains this set. Example: >>> OrderedSet([1, 2, 3]).issubset({1, 2}) False >>> OrderedSet([1, 2, 3]).issubset({1, 2, 3, 4}) True >>> OrderedSet([1, 2, 3]).issubset({1, 4, 3, 5}) False

def issuperset(self, other): (source)

Report whether this set contains another set. Example: >>> OrderedSet([1, 2]).issuperset([1, 2, 3]) False >>> OrderedSet([1, 2, 3, 4]).issuperset({1, 2, 3}) True >>> OrderedSet([1, 4, 3, 5]).issuperset({1, 2, 3}) False

def pop(self): (source)

Remove and return the last element from the set. Raises KeyError if the set is empty. Example: >>> oset = OrderedSet([1, 2, 3]) >>> oset.pop() 3

def symmetric_difference(self, other): (source)

Return the symmetric difference of two OrderedSets as a new set. That is, the new set will contain all elements that are in exactly one of the sets. Their order will be preserved, with elements from `self` preceding elements from `other`. Example: >>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.symmetric_difference(other) OrderedSet([4, 5, 9, 2])

def symmetric_difference_update(self, other): (source)

Update this OrderedSet to remove items from another set, then add items from the other set that were not present in this set. Example: >>> this = OrderedSet([1, 4, 3, 5, 7]) >>> other = OrderedSet([9, 7, 1, 3, 2]) >>> this.symmetric_difference_update(other) >>> print(this) OrderedSet([4, 5, 9, 2])

def union(self, *sets): (source)

Combines all unique items. Each items order is defined by its first appearance. Example: >>> oset = OrderedSet.union(OrderedSet([3, 1, 4, 1, 5]), [1, 3], [2, 0]) >>> print(oset) OrderedSet([3, 1, 4, 5, 2, 0]) >>> oset.union([8, 9]) OrderedSet([3, 1, 4, 5, 2, 0, 8, 9]) >>> oset | {10} OrderedSet([3, 1, 4, 5, 2, 0, 10])

def update(self, sequence): (source)

Update the set with the given iterable sequence, then return the index of the last element inserted. Example: >>> oset = OrderedSet([1, 2, 3]) >>> oset.update([3, 1, 5, 1, 4]) 4 >>> print(oset) OrderedSet([1, 2, 3, 5, 4])

Undocumented

Undocumented

def _update_items(self, items): (source)

Replace the 'items' list of this OrderedSet with a new one, updating self.map accordingly.