set

Report an issue View source Nightly · 7.4 . 7.3 · 7.2 · 7.1 · 7.0 · 6.5

Experimental. This API is experimental and may change at any time. Please do not depend on it. It may be enabled on an experimental basis by setting --experimental_enable_starlark_set.

The built-in mutable set type. Example set expressions:

x = set()           # x is an empty set
y = set([1, 2, 3])  # y is a set with 3 elements
3 in y              # True
0 in y              # False
len(x)              # 0
len(y)              # 3

A set used in Boolean context is true if and only if it is non-empty.

s = set()
"non-empty" if s else "empty"  # "empty"
t = set(["x", "y"])
"non-empty" if t else "empty"  # "non-empty"

The elements of a set must be hashable; x may be an element of a set if and only if x may be used as a key of a dict.

A set itself is not hashable; therefore, you cannot have a set with another set as an element.

You cannot access the elements of a set by index, but you can iterate over them, and you can obtain the list of a set's elements in iteration order using the list() built-in function. Just like for lists, it is an error to mutate a set while it is being iterated over. The order of iteration matches insertion order:

s = set([3, 1, 3])
s.add(2)
# prints 3, 1, 2
for item in s:
    print(item)
list(s)  # [3, 1, 2]

A set s is equal to t if and only if t is a set containing the same elements, possibly with a different iteration order. In particular, a set is not equal to its list of elements.

Sets are not ordered; the <, <=, >, and >= operations are not defined for sets, and a list of sets cannot be sorted - unlike in Python.

The | operation on two sets returns the union of the two sets: a set containing the elements found in either one or both of the original sets. The | operation has an augmented assignment version; s |= t adds to s all the elements of t.

set([1, 2]) | set([3, 2])  # set([1, 2, 3])
s = set([1, 2])
s |= set([2, 3, 4])        # s now equals set([1, 2, 3, 4])

The & operation on two sets returns the intersection of the two sets: a set containing only the elements found in both of the original sets. The & operation has an augmented assignment version; s &= t removes from s all the elements not found in t.

set([1, 2]) & set([2, 3])  # set([2])
set([1, 2]) & set([3, 4])  # set()
s = set([1, 2])
s &= set([0, 1])           # s now equals set([1])

The - operation on two sets returns the difference of the two sets: a set containing the elements found in the left-hand side set but not the right-hand site set. The - operation has an augmented assignment version; s -= t removes from s all the elements found in t.

set([1, 2]) - set([2, 3])  # set([1])
set([1, 2]) - set([3, 4])  # set([1, 2])
s = set([1, 2])
s -= set([0, 1])           # s now equals set([2])

The ^ operation on two sets returns the symmetric difference of the two sets: a set containing the elements found in exactly one of the two original sets, but not in both. The ^ operation has an augmented assignment version; s ^= t removes from s any element of t found in s and adds to s any element of t not found in s.

set([1, 2]) ^ set([2, 3])  # set([1, 3])
set([1, 2]) ^ set([3, 4])  # set([1, 2, 3, 4])
s = set([1, 2])
s ^= set([0, 1])           # s now equals set([2, 0])

Members

add

None set.add(element)

Adds an element to the set.

Parameters

Parameter Description
element required
Element to add.

clear

None set.clear()

Removes all the elements of the set.

difference

set set.difference(*others)

Returns a new mutable set containing the difference of this set with others.

For example,

set([1, 2, 3]).intersection([1, 2], [2, 3]) == set([2])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.

difference_update

None set.difference_update(*others)

Removes any elements found in any others from this set.

For example,

x = set([1, 2, 3, 4])
x.difference_update([2, 3], [3, 4])
# x is now set([1])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.

discard

None set.discard(element)

Removes an element from the set if it is present.

Parameters

Parameter Description
element required
Element to discard.

intersection

set set.intersection(*others)

Returns a new mutable set containing the intersection of this set with others.

For example,

set([1, 2, 3]).intersection([1, 2], [2, 3]) == set([2])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.

intersection_update

None set.intersection_update(*others)

Removes any elements not found in all others from this set.

For example,

x = set([1, 2, 3, 4])
x.intersection_update([2, 3], [3, 4])
# x is now set([3])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.

isdisjoint

bool set.isdisjoint(other)

Returns true if this set has no elements in common with another.

For example,

set([1, 2]).isdisjoint([3, 4]) == True
set().isdisjoint(set()) == True
set([1, 2]).isdisjoint([2, 3]) == False

Parameters

Parameter Description
other required
A set, sequence, or dict.

issubset

bool set.issubset(other)

Returns true of this set is a subset of another.

For example,

set([1, 2]).issubset([1, 2, 3]) == True
set([1, 2]).issubset([1, 2]) == True
set([1, 2]).issubset([2, 3]) == False

Parameters

Parameter Description
other required
A set, sequence, or dict.

issuperset

bool set.issuperset(other)

Returns true of this set is a superset of another.

For example,

set([1, 2, 3]).issuperset([1, 2]) == True
set([1, 2, 3]).issuperset([1, 2, 3]) == True
set([1, 2, 3]).issuperset([2, 3, 4]) == False

Parameters

Parameter Description
other required
A set, sequence, or dict.

pop

unknown set.pop()

Removes and returns the first element of the set. Fails if the set is empty.

remove

None set.remove(element)

Removes an element, which must be present in the set, from the set. Fails if the element was not present in the set.

Parameters

Parameter Description
element required
Element to remove.

symmetric_difference

set set.symmetric_difference(other)

Returns a new mutable set containing the symmetric difference of this set with another set, sequence, or dict.

For example,

set([1, 2, 3]).symmetric_difference([2, 3, 4]) == set([1, 4])

Parameters

Parameter Description
other required
A set, sequence, or dict.

symmetric_difference_update

None set.symmetric_difference_update(other)

Returns a new mutable set containing the symmetric difference of this set with another set, sequence, or dict.

For example,

set([1, 2, 3]).symmetric_difference([2, 3, 4]) == set([1, 4])

Parameters

Parameter Description
other required
A set, sequence, or dict.

union

set set.union(*others)

Returns a new mutable set containing the union of this set with others.

For example,

set([1, 2]).union([2, 3, 4], [4, 5]) == set([1, 2, 3, 4, 5])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.

update

None set.update(*others)

Adds the elements found in others to this set.

For example,

x = set([1, 2])
x.update([2, 3], [3, 4])
# x is now set([1, 2, 3, 4])

Parameters

Parameter Description
others required
Sets, sequences, or dicts.