set¶
A mutable set of unique elements, similar to Python's set.
Supports set operations: union, intersection, difference, and symmetric difference.
Properties¶
| Name | Type | Description |
|---|---|---|
count |
int |
Gets the number of elements in the set. |
is_read_only |
bool |
Gets a value indicating whether the set is read-only. |
Methods¶
copy() -> set[T]¶
Return a shallow copy of the set.
Returns: A new set with the same elements.
is_proper_subset(other: set[T]) -> bool¶
Returns whether this set is a proper subset of other (subset but not equal).
Parameters:
other(set[T]) -- The set to compare against
Returns: True if this set is a proper subset of other
is_subset(other: set[T]) -> bool¶
Returns whether this set is a subset of other (all elements in other).
Parameters:
other(set[T]) -- The set to compare against.
Returns: True if every element in this set is also in other.
is_subset(other: IEnumerable[T]) -> bool¶
Returns whether this set is a subset of the given iterable.
is_proper_superset(other: set[T]) -> bool¶
Returns whether this set is a proper superset of other (superset but not equal).
Parameters:
other(set[T]) -- The set to compare against
Returns: True if this set is a proper superset of other
is_superset(other: set[T]) -> bool¶
Returns whether this set is a superset of other (contains all elements of other).
Parameters:
other(set[T]) -- The set to compare against.
Returns: True if every element in other is also in this set.
is_superset(other: IEnumerable[T]) -> bool¶
Returns whether this set is a superset of the given iterable.
union(other: set[T]) -> set[T]¶
Returns a new set with elements from both sets.
Parameters:
other(set[T]) -- The other set.
Returns: A new set containing elements from both sets.
union(other: IEnumerable[T]) -> set[T]¶
Return a new set with elements from this set and the iterable.
union(*others: IEnumerable[T]) -> set[T]¶
Return a new set with elements from this set and all others.
intersection(other: set[T]) -> set[T]¶
Returns a new set with elements common to both sets.
Parameters:
other(set[T]) -- The other set.
Returns: A new set containing only elements found in both sets.
intersection(other: IEnumerable[T]) -> set[T]¶
Return a new set with elements common to this set and the iterable.
intersection(*others: IEnumerable[T]) -> set[T]¶
Return a new set with elements common to this set and all others.
difference(other: set[T]) -> set[T]¶
Returns a new set with elements in this set but not in other.
Parameters:
other(set[T]) -- The other set.
Returns: A new set with elements only in this set.
difference(other: IEnumerable[T]) -> set[T]¶
Return a new set with elements in this set but not in the iterable.
difference(*others: IEnumerable[T]) -> set[T]¶
Return a new set with elements in this set but not in any of the others.
symmetric_difference(other: set[T]) -> set[T]¶
Returns a new set with elements in either set but not both.
Parameters:
other(set[T]) -- The other set.
Returns: A new set with elements in exactly one of the two sets.
symmetric_difference(other: IEnumerable[T]) -> set[T]¶
Return a new set with elements in either this set or the iterable but not both.
to_hash_set() -> HashSet[T]¶
Convert to a standard .NET HashSet.
add(x: T)¶
Add an element to the set (no effect if already present).
Parameters:
x(T) -- The element to add.
Note
For initializer literals and part of ICollection interface.
discard(x: T)¶
Remove an element from the set if present (no error if not present).
Parameters:
x(T) -- The element to discard.
clear()¶
Remove all elements from the set.
pop() -> T¶
Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.
Returns: An arbitrary element from the set.
Raises:
KeyError-- Thrown if the set is empty.
remove(x: T)¶
Remove an element from the set. Raises KeyError if the element is not present.
Parameters:
x(T) -- The element to remove.
Raises:
KeyError-- Thrown if the element is not found.
contains(x: T) -> bool¶
Returns whether the item is in the set.
Parameters:
x(T) -- The element to check for.
Returns: True if the element is found; otherwise False.
is_disjoint(other: set[T]) -> bool¶
Returns whether this set has no elements in common with other.
Parameters:
other(set[T]) -- The set to test against.
Returns: True if the sets have no common elements.
is_disjoint(other: IEnumerable[T]) -> bool¶
Returns whether this set has no elements in common with the given iterable.
update(*others: IEnumerable[T])¶
Update the set, adding elements from the given iterables.
Parameters:
others(*IEnumerable[T]) -- The iterables of elements to add.
difference_update(*others: IEnumerable[T])¶
Update the set, removing elements found in any of the given iterables.
Parameters:
others(*IEnumerable[T]) -- The iterables of elements to remove.
intersection_update(*others: IEnumerable[T])¶
Update the set, keeping only elements found in all given iterables.
Parameters:
others(*IEnumerable[T]) -- The iterables to intersect with.
s = {1, 2, 3}
s.intersection_update({2, 3, 4}) # {2, 3}
s.intersection_update({1, 2, 3}, {2, 3}) # {2, 3}
symmetric_difference_update(other: set[T])¶
Update the set, keeping only elements found in either set but not both.
Parameters:
other(set[T]) -- The set to compute symmetric difference with.
symmetric_difference_update(other: IEnumerable[T])¶
Update the set, keeping only elements found in either set or the iterable but not both.
Parameters:
other(IEnumerable[T]) -- The iterable to compute symmetric difference with.
__str__() -> str¶
repr() uses the same method. Python's repr of the set: {1, 2}, and set() when empty (a bare
{} is the empty dict).
copy_to(array: array[T], array_index: int)¶
Copies the elements of the set to an array.
except_with(other: IEnumerable[T])¶
Removes all elements in the specified collection from the current set.
intersect_with(other: IEnumerable[T])¶
Modifies the current set to contain only elements present in both sets.
is_proper_subset_of(other: IEnumerable[T]) -> bool¶
Determines whether the current set is a proper subset of the specified collection.
is_proper_superset_of(other: IEnumerable[T]) -> bool¶
Determines whether the current set is a proper superset of the specified collection.
is_subset_of(other: IEnumerable[T]) -> bool¶
Determines whether the current set is a subset of the specified collection.
is_superset_of(other: IEnumerable[T]) -> bool¶
Determines whether the current set is a superset of the specified collection.
overlaps(other: IEnumerable[T]) -> bool¶
Determines whether the current set and a specified collection share common elements.
set_equals(other: IEnumerable[T]) -> bool¶
Determines whether the current set and the specified collection contain the same elements.
symmetric_except_with(other: IEnumerable[T])¶
Modifies the current set to contain only elements present in either the current set or the specified collection, but not both.
union_with(other: IEnumerable[T])¶
Modifies the current set to contain all elements present in either the current set or the specified collection.