IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

Set

struct Set[T: Movable & Hashable & Equatable & ImplicitlyDestructible, H: Hasher = AHasher[SIMD[DType.uint64, 4](0)]]

A set data type.

O(1) average-case amortized add, remove, and membership check.

from std.collections import Set

var set = { 1, 2, 3 }
print(len(set)) # 3
set.add(4)

for element in set:
print(element)

set -= Set[Int](3, 4, 5)
print(set == Set[Int](1, 2)) # True
print(set | Set[Int](0, 1) == Set[Int](0, 1, 2)) # True
var element = set.pop()
print(len(set)) # 1

Parameters

  • T (Movable & Hashable & Equatable & ImplicitlyDestructible): The element type of the set. Must implement KeyElement (i.e. Movable & Hashable & Equatable). Methods that fundamentally need to copy elements (union, intersection, __or__, iteration, ...) are conditionally available via where conforms_to(T, Copyable) clauses.
  • H (Hasher): The type of the hasher used to hash keys.

Implemented traits

AnyType, Boolable, Comparable, Copyable, Equatable, Hashable, ImplicitlyDestructible, Iterable, IterableOwned, Movable, Sized, Writable

comptime members

IteratorOwnedType

comptime IteratorOwnedType = _DictKeyIterOwned[T(ImplicitlyDestructible & Equatable & Copyable & Movable & Hashable), NoneType, H]

The owned iterator type for this set.

IteratorType

comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = _DictKeyIter[T(ImplicitlyDestructible & Equatable & Copyable & Movable & Hashable), NoneType, H, iterable_origin]

The iterator type for this set.

Parameters

Methods

__init__

def __init__(out self)

Construct an empty set.

def __init__(out self, *ts: T, *, __set_literal__: NoneType = None) where conforms_to(T, AnyType & Copyable & Movable)

Construct a set from initial elements.

Args:

  • *ts (T): Variadic of elements to add to the set.
  • set_literal (NoneType): Tell Mojo to use this method for set literals.

def __init__(out self, elements: List[T]) where conforms_to(T, AnyType & Copyable & Movable)

Construct a set from a List of elements.

Args:

  • elements (List[T]): A vector of elements to add to the set.

__bool__

def __bool__(self) -> Bool

Whether the set is non-empty or not.

Returns:

Bool: True if the set is non-empty, False if it is empty.

__lt__

def __lt__(self, other: Self) -> Bool where conforms_to(T, AnyType & Equatable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Overloads the < operator for strict subset comparison of sets.

Args:

  • other (Self): The set to compare against for the strict subset relationship.

Returns:

Bool: True if the set is a strict subset of the other set, False otherwise.

__le__

def __le__(self, other: Self) -> Bool where conforms_to(T, AnyType & Equatable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Overloads the <= operator for sets. Works like as issubset method.

Args:

  • other (Self): Another Set instance to check against.

Returns:

Bool: True if this set is a subset of the other set, False otherwise.

__eq__

def __eq__(self, other: Self) -> Bool where conforms_to(T, AnyType & Equatable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Set equality.

Args:

  • other (Self): Another Set instance to check equality against.

Returns:

Bool: True if the sets contain the same elements and False otherwise.

__gt__

def __gt__(self, other: Self) -> Bool where conforms_to(T, AnyType & Equatable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Overloads the > operator for strict superset comparison of sets.

Args:

  • other (Self): The set to compare against for the strict superset relationship.

Returns:

Bool: True if the set is a strict superset of the other set, False otherwise.

__ge__

def __ge__(self, other: Self) -> Bool where conforms_to(T, AnyType & Equatable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Overloads the >= operator for sets. Works like as issuperset method.

Args:

  • other (Self): Another Set instance to check against.

Returns:

Bool: True if this set is a superset of the other set, False otherwise.

__contains__

def __contains__(self, t: T) -> Bool

Whether or not the set contains an element.

Args:

  • t (T): The element to check membership in the set.

Returns:

Bool: Whether or not the set contains the element.

__sub__

def __sub__(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Set subtraction.

Args:

  • other (Self): Another Set instance to subtract from this one.

Returns:

Self: A new set containing elements of this set, but not containing any elements which were in the other set.

__and__

def __and__(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

The set intersection operator.

Args:

  • other (Self): Another Set instance to intersect with this one.

Returns:

Self: A new set containing only the elements which appear in both this set and the other set.

__or__

def __or__(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

The set union operator.

Args:

  • other (Self): Another Set instance to union with this one.

Returns:

Self: A new set containing any elements which appear in either this set or the other set.

__xor__

def __xor__(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Overloads the ^ operator for sets. Works like as symmetric_difference method.

Args:

  • other (Self): The set to find the symmetric difference with.

Returns:

Self: A new set containing the symmetric difference of the two sets.

__isub__

def __isub__(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set subtraction.

Updates the set to remove any elements from the other set.

Args:

  • other (Self): Another Set instance to subtract from this one.

__iand__

def __iand__(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set intersection.

Updates the set to contain only the elements which are already in the set and are also contained in the other set.

Args:

  • other (Self): Another Set instance to intersect with this one.

__ixor__

def __ixor__(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

Overloads the ^= operator. Works like as symmetric_difference_update method.

Updates the set with the symmetric difference of itself and another set.

Args:

  • other (Self): The set to find the symmetric difference with.

__ior__

def __ior__(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set union.

Updates the set to contain all elements in the other set as well as keeping all elements it already contained.

Args:

  • other (Self): Another Set instance to union with this one.

__len__

def __len__(self) -> Int

The size of the set.

Returns:

Int: The number of elements in the set.

__hash__

def __hash__(self, mut hasher: T) where conforms_to(T, AnyType & Hashable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Updates hasher with the underlying values.

The update is order independent, so s1 == s2 -> hash(s1) == hash(s2).

Args:

  • hasher (T): The hasher instance.

write_to

def write_to(self, mut writer: T) where conforms_to(T, AnyType & Writable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Write this set to a Writer.

Args:

  • writer (T): The object to write to.

write_repr_to

def write_repr_to(self, mut writer: T) where conforms_to(T, AnyType & Writable) if conforms_to(T, AnyType & Copyable & Movable) else conforms_to(T, AnyType & Copyable & Movable)

Write this set to a Writer.

Args:

  • writer (T): The object to write to.

__iter__

def __iter__(deinit self) -> Self.IteratorOwnedType

Consume the set and iterate over its elements.

Returns:

Self.IteratorOwnedType: An iterator that owns the set's elements.

def __iter__(ref self) -> _DictKeyIter[T(ImplicitlyDestructible & Equatable & Copyable & Movable & Hashable), NoneType, H, origin_of(self)]

Iterate over elements of the set, returning immutable references.

Returns:

_DictKeyIter[T(ImplicitlyDestructible & Equatable & Copyable & Movable & Hashable), NoneType, H, origin_of(self)]: An iterator of immutable references to the set elements.

add

def add(mut self, var t: T)

Add an element to the set.

Args:

  • t (T): The element to add to the set.

remove

def remove(mut self, t: T)

Remove an element from the set.

Args:

  • t (T): The element to remove from the set.

Raises:

If the element isn't in the set to remove.

pop

def pop(mut self) -> T

Remove any one item from the set, and return it.

As an implementation detail this will remove the last item according to insertion order.

Returns:

T: The element which was removed from the set.

Raises:

If the set is empty.

union

def union(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Set union.

Args:

  • other (Self): Another Set instance to union with this one.

Returns:

Self: A new set containing any elements which appear in either this set or the other set.

intersection

def intersection(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Set intersection.

Args:

  • other (Self): Another Set instance to intersect with this one.

Returns:

Self: A new set containing only the elements which appear in both this set and the other set.

difference

def difference(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Set difference.

Args:

  • other (Self): Another Set instance to find the difference with this one.

Returns:

Self: A new set containing elements that are in this set but not in the other set.

update

def update(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set update.

Updates the set to contain all elements in the other set as well as keeping all elements it already contained.

Args:

  • other (Self): Another Set instance to union with this one.

intersection_update

def intersection_update(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set intersection update.

Updates the set by retaining only elements found in both this set and the other set, removing all other elements. The result is the intersection of this set with other.

Args:

  • other (Self): Another Set instance to intersect with this one.

difference_update

def difference_update(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

In-place set subtraction.

Updates the set by removing all elements found in the other set, effectively keeping only elements that are unique to this set.

Args:

  • other (Self): Another Set instance to subtract from this one.

issubset

def issubset(self, other: Self) -> Bool where conforms_to(T, AnyType & Copyable & Movable)

Check if this set is a subset of another set.

Args:

  • other (Self): Another Set instance to check against.

Returns:

Bool: True if this set is a subset of the other set, False otherwise.

isdisjoint

def isdisjoint(self, other: Self) -> Bool where conforms_to(T, AnyType & Copyable & Movable)

Check if this set is disjoint with another set.

Args:

  • other (Self): Another Set instance to check against.

Returns:

Bool: True if this set is disjoint with the other set, False otherwise.

issuperset

def issuperset(self, other: Self) -> Bool where conforms_to(T, AnyType & Copyable & Movable)

Check if this set is a superset of another set.

Args:

  • other (Self): Another Set instance to check against.

Returns:

Bool: True if this set is a superset of the other set, False otherwise.

symmetric_difference

def symmetric_difference(self, other: Self) -> Self where conforms_to(T, AnyType & Copyable & Movable)

Returns the symmetric difference of two sets.

Args:

  • other (Self): The set to find the symmetric difference with.

Returns:

Self: A new set containing the symmetric difference of the two sets.

symmetric_difference_update

def symmetric_difference_update(mut self, other: Self) where conforms_to(T, AnyType & Copyable & Movable)

Updates the set with the symmetric difference of itself and another set.

Args:

  • other (Self): The set to find the symmetric difference with.

discard

def discard(mut self, value: T)

Remove a value from the set if it exists. Pass otherwise.

Args:

  • value (T): The element to remove from the set.

clear

def clear(mut self)

Removes all elements from the set.

This method modifies the set in-place, removing all of its elements. After calling this method, the set will be empty.