blob: d841c5585b0f6a42fe65213cbf158ed343da89aa [file]
<html devsite>
<head>
<meta name="project_path" value="/_project.yaml">
<meta name="book_path" value="/versions/8.4.0/_book.yaml">
</head>
<body>
<h1 class="page-title" id="modules.set">set</h1>
{% dynamic setvar source_file "src/main/java/net/starlark/java/eval/StarlarkSet.java" %}
{% dynamic setvar version "8.4.0" %}
{% dynamic setvar original_path "/rules/lib/core/set" %}
{% include "_buttons.html" %}
<!-- {% raw %} -->
The built-in set type. A set is a mutable, iterable collection of unique values &ndash; the set's
<em>elements</em>. The <a href="../globals/all#type">type name</a> of a set is <code>"set"</code>.
<p>Sets provide constant-time operations to insert, remove, or check for the presence of a value.
Sets are implemented using a hash table, and therefore, just like keys of a
<a href="../dict">dictionary</a>, elements of a set must be hashable. A value may be used as an
element of a set if and only if it may be used as a key of a dictionary.
<p>Sets may be constructed using the <a href="../globals/all#set"><code>set()</code></a> built-in
function, which returns a new set containing the unique elements of its optional argument, which
must be an iterable. Calling <code>set()</code> without an argument constructs an empty set. Sets
have no literal syntax.
<p>The <code>in</code> and <code>not in</code> operations check whether a value is (or is not) in a
set:
<pre class=language-python>
s = set(["a", "b", "c"])
"a" in s # True
"z" in s # False
</pre>
<p>A set is iterable, and thus may be used as the operand of a <code>for</code> loop, a list
comprehension, and the various built-in functions that operate on iterables. Its length can be
retrieved using the <a href="../globals/all#len"><code>len()</code></a> built-in function, and the
order of iteration is the order in which elements were first added to the set:
<pre class=language-python>
s = set(["z", "y", "z", "y"])
len(s) # prints 2
s.add("x")
len(s) # prints 3
for e in s:
print e # prints "z", "y", "x"
</pre>
<p>A set used in Boolean context is true if and only if it is non-empty.
<pre class=language-python>
s = set()
"non-empty" if s else "empty" # "empty"
t = set(["x", "y"])
"non-empty" if t else "empty" # "non-empty"
</pre>
<p>Sets may be compared for equality or inequality using <code>==</code> and <code>!=</code>. A set
<code>s</code> is equal to <code>t</code> if and only if <code>t</code> is a set containing the same
elements; iteration order is not significant. In particular, a set is <em>not</em> equal to the list
of its elements. Sets are not ordered with respect to other sets, and an attempt to compare two sets
using <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code>, or to sort a
sequence of sets, will fail.
<pre class=language-python>
set() == set() # True
set() != [] # True
set([1, 2]) == set([2, 1]) # True
set([1, 2]) != [1, 2] # True
</pre>
<p>The <code>|</code> 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.
<pre class=language-python>
set([1, 2]) | set([3, 2]) # set([1, 2, 3])
</pre>
<p>The <code>&amp;</code> operation on two sets returns the intersection of the two sets: a set
containing only the elements found in both of the original sets.
<pre class=language-python>
set([1, 2]) &amp; set([2, 3]) # set([2])
set([1, 2]) &amp; set([3, 4]) # set()
</pre>
<p>The <code>-</code> 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 side set.
<pre class=language-python>
set([1, 2]) - set([2, 3]) # set([1])
set([1, 2]) - set([3, 4]) # set([1, 2])
</pre>
<p>The <code>^</code> 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.
<pre class=language-python>
set([1, 2]) ^ set([2, 3]) # set([1, 3])
set([1, 2]) ^ set([3, 4]) # set([1, 2, 3, 4])
</pre>
<p>In each of the above operations, the elements of the resulting set retain their order from the
two operand sets, with all elements that were drawn from the left-hand side ordered before any
element that was only present in the right-hand side.
<p>The corresponding augmented assignments, <code>|=</code>, <code>&amp;=</code>, <code>-=</code>,
and <code>^=</code>, modify the left-hand set in place.
<pre class=language-python>
s = set([1, 2])
s |= set([2, 3, 4]) # s now equals set([1, 2, 3, 4])
s &amp;= set([0, 1, 2, 3]) # s now equals set([1, 2, 3])
s -= set([0, 1]) # s now equals set([2, 3])
s ^= set([3, 4]) # s now equals set([2, 4])
</pre>
<p>Like all mutable values in Starlark, a set can be frozen, and once frozen, all subsequent
operations that attempt to update it will fail.
<h2>Members</h2>
<ul>
<li>
<a href="#add">add</a>
</li>
<li>
<a href="#clear">clear</a>
</li>
<li>
<a href="#difference">difference</a>
</li>
<li>
<a href="#difference_update">difference_update</a>
</li>
<li>
<a href="#discard">discard</a>
</li>
<li>
<a href="#intersection">intersection</a>
</li>
<li>
<a href="#intersection_update">intersection_update</a>
</li>
<li>
<a href="#isdisjoint">isdisjoint</a>
</li>
<li>
<a href="#issubset">issubset</a>
</li>
<li>
<a href="#issuperset">issuperset</a>
</li>
<li>
<a href="#pop">pop</a>
</li>
<li>
<a href="#remove">remove</a>
</li>
<li>
<a href="#symmetric_difference">symmetric_difference</a>
</li>
<li>
<a href="#symmetric_difference_update">symmetric_difference_update</a>
</li>
<li>
<a href="#union">union</a>
</li>
<li>
<a href="#update">update</a>
</li>
</ul>
<h2 id="add">add</h2>
<p><pre class="rule-signature"><code>None</code> set.add(element)</pre></p>
Adds an element to the set.
<p>It is permissible to <code>add</code> a value already present in the set; this leaves the set
unchanged.
<p>If you need to add multiple elements to a set, see <a href="#update"><code>update</code></a> or
the <code>|=</code> augmented assignment operation.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="add.element">
<code>element</code>
</td>
<td>
required<br/>
Element to add.
</td>
</tr>
</tbody>
</table>
<h2 id="clear">clear</h2>
<p><pre class="rule-signature"><code>None</code> set.clear()</pre></p>
Removes all the elements of the set.
<h2 id="difference">difference</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/set.html">set</a> set.difference(*others)</pre></p>
Returns a new mutable set containing the difference of this set with others.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.difference(t)</code> is equivalent to
<code>s - t</code>; however, note that the <code>-</code> operation requires both sides to be sets,
while the <code>difference</code> method also accepts sequences and dicts.
<p>It is permissible to call <code>difference</code> without any arguments; this returns a copy of
the set.
<p>For example,
<pre class=language-python>
set([1, 2, 3]).difference([2]) # set([1, 3])
set([1, 2, 3]).difference([0, 1], [3, 4]) # set([2])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="difference.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
<h2 id="difference_update">difference_update</h2>
<p><pre class="rule-signature"><code>None</code> set.difference_update(*others)</pre></p>
Removes any elements found in any others from this set.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.difference_update(t)</code> is equivalent
to <code>s -= t</code>; however, note that the <code>-=</code> augmented assignment requires both
sides to be sets, while the <code>difference_update</code> method also accepts sequences and dicts.
<p>It is permissible to call <code>difference_update</code> without any arguments; this leaves the
set unchanged.
<p>For example,
<pre class=language-python>
s = set([1, 2, 3, 4])
s.difference_update([2]) # None; s is set([1, 3, 4])
s.difference_update([0, 1], [4, 5]) # None; s is set([3])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="difference_update.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
<h2 id="discard">discard</h2>
<p><pre class="rule-signature"><code>None</code> set.discard(element)</pre></p>
Removes an element from the set if it is present.
<p>It is permissible to <code>discard</code> a value not present in the set; this leaves the set
unchanged. If you want to fail on an attempt to remove a non-present element, use
<a href="#remove"><code>remove</code></a> instead. If you need to remove multiple elements from a
set, see <a href="#difference_update"><code>difference_update</code></a> or the <code>-=</code>
augmented assignment operation.
<p>For example,
<pre class=language-python>
s = set(["x", "y"])
s.discard("y") # None; s == set(["x"])
s.discard("y") # None; s == set(["x"])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="discard.element">
<code>element</code>
</td>
<td>
required<br/>
Element to discard. Must be hashable.
</td>
</tr>
</tbody>
</table>
<h2 id="intersection">intersection</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/set.html">set</a> set.intersection(*others)</pre></p>
Returns a new mutable set containing the intersection of this set with others.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.intersection(t)</code> is equivalent to
<code>s &amp; t</code>; however, note that the <code>&amp;</code> operation requires both sides to
be sets, while the <code>intersection</code> method also accepts sequences and dicts.
<p>It is permissible to call <code>intersection</code> without any arguments; this returns a copy of
the set.
<p>For example,
<pre class=language-python>
set([1, 2]).intersection([2, 3]) # set([2])
set([1, 2, 3]).intersection([0, 1], [1, 2]) # set([1])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="intersection.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
<h2 id="intersection_update">intersection_update</h2>
<p><pre class="rule-signature"><code>None</code> set.intersection_update(*others)</pre></p>
Removes any elements not found in all others from this set.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.intersection_update(t)</code> is
equivalent to <code>s &amp;= t</code>; however, note that the <code>&amp;=</code> augmented
assignment requires both sides to be sets, while the <code>intersection_update</code> method also
accepts sequences and dicts.
<p>It is permissible to call <code>intersection_update</code> without any arguments; this leaves the
set unchanged.
<p>For example,
<pre class=language-python>
s = set([1, 2, 3, 4])
s.intersection_update([0, 1, 2]) # None; s is set([1, 2])
s.intersection_update([0, 1], [1, 2]) # None; s is set([1])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="intersection_update.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
<h2 id="isdisjoint">isdisjoint</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/bool.html">bool</a> set.isdisjoint(other)</pre></p>
Returns true if this set has no elements in common with another.
<p>For example,
<pre class=language-python>
set([1, 2]).isdisjoint([3, 4]) # True
set().isdisjoint(set()) # True
set([1, 2]).isdisjoint([2, 3]) # False
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="isdisjoint.other">
<code>other</code>
</td>
<td>
required<br/>
A set, a sequence of hashable elements, or a dict.
</td>
</tr>
</tbody>
</table>
<h2 id="issubset">issubset</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/bool.html">bool</a> set.issubset(other)</pre></p>
Returns true of this set is a subset of another.
<p>Note that a set is always considered to be a subset of itself.
<p>For example,
<pre class=language-python>
set([1, 2]).issubset([1, 2, 3]) # True
set([1, 2]).issubset([1, 2]) # True
set([1, 2]).issubset([2, 3]) # False
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="issubset.other">
<code>other</code>
</td>
<td>
required<br/>
A set, a sequence of hashable elements, or a dict.
</td>
</tr>
</tbody>
</table>
<h2 id="issuperset">issuperset</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/bool.html">bool</a> set.issuperset(other)</pre></p>
Returns true of this set is a superset of another.
<p>Note that a set is always considered to be a superset of itself.
<p>For example,
<pre class=language-python>
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
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="issuperset.other">
<code>other</code>
</td>
<td>
required<br/>
A set, a sequence of hashable elements, or a dict.
</td>
</tr>
</tbody>
</table>
<h2 id="pop">pop</h2>
<p><pre class="rule-signature">unknown set.pop()</pre></p>
Removes and returns the first element of the set (in iteration order, which is the order in which
elements were first added to the set).
<p>Fails if the set is empty.
<p>For example,
<pre class=language-python>
s = set([3, 1, 2])
s.pop() # 3; s == set([1, 2])
s.pop() # 1; s == set([2])
s.pop() # 2; s == set()
s.pop() # error: empty set
</pre>
<h2 id="remove">remove</h2>
<p><pre class="rule-signature"><code>None</code> set.remove(element)</pre></p>
Removes an element, which must be present in the set, from the set.
<p><code>remove</code> fails if the element was not present in the set. If you don't want to fail on
an attempt to remove a non-present element, use <a href="#discard"><code>discard</code></a> instead.
If you need to remove multiple elements from a set, see
<a href="#difference_update"><code>difference_update</code></a> or the <code>-=</code> augmented
assignment operation.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="remove.element">
<code>element</code>
</td>
<td>
required<br/>
Element to remove. Must be an element of the set (and hashable).
</td>
</tr>
</tbody>
</table>
<h2 id="symmetric_difference">symmetric_difference</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/set.html">set</a> set.symmetric_difference(other)</pre></p>
Returns a new mutable set containing the symmetric difference of this set with another set,
sequence, or dict.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.symmetric_difference(t)</code> is
equivalent to <code>s ^ t</code>; however, note that the <code>^</code> operation requires both
sides to be sets, while the <code>symmetric_difference</code> method also accepts a sequence or a
dict.
<p>For example,
<pre class=language-python>
set([1, 2]).symmetric_difference([2, 3]) # set([1, 3])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="symmetric_difference.other">
<code>other</code>
</td>
<td>
required<br/>
A set, a sequence of hashable elements, or a dict.
</td>
</tr>
</tbody>
</table>
<h2 id="symmetric_difference_update">symmetric_difference_update</h2>
<p><pre class="rule-signature"><code>None</code> set.symmetric_difference_update(other)</pre></p>
Returns a new mutable set containing the symmetric difference of this set with another set,
sequence, or dict.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.symmetric_difference_update(t)</code> is
equivalent to `s ^= t<code>; however, note that the </code>^=` augmented assignment requires both
sides to be sets, while the <code>symmetric_difference_update</code> method also accepts a sequence
or a dict.
<p>For example,
<pre class=language-python>
s = set([1, 2])
s.symmetric_difference_update([2, 3]) # None; s == set([1, 3])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="symmetric_difference_update.other">
<code>other</code>
</td>
<td>
required<br/>
A set, a sequence of hashable elements, or a dict.
</td>
</tr>
</tbody>
</table>
<h2 id="union">union</h2>
<p><pre class="rule-signature"><a class="anchor" href="../core/set.html">set</a> set.union(*others)</pre></p>
Returns a new mutable set containing the union of this set with others.
<p>If <code>s</code> and <code>t</code> are sets, <code>s.union(t)</code> is equivalent to
<code>s | t</code>; however, note that the <code>|</code> operation requires both sides to be sets,
while the <code>union</code> method also accepts sequences and dicts.
<p>It is permissible to call <code>union</code> without any arguments; this returns a copy of the
set.
<p>For example,
<pre class=language-python>
set([1, 2]).union([2, 3]) # set([1, 2, 3])
set([1, 2]).union([2, 3], {3: "a", 4: "b"}) # set([1, 2, 3, 4])
</pre>
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="union.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
<h2 id="update">update</h2>
<p><pre class="rule-signature"><code>None</code> set.update(*others)</pre></p>
Adds the elements found in others to this set.
<p>For example,
<pre class=language-python>
s = set()
s.update([1, 2]) # None; s is set([1, 2])
s.update([2, 3], [3, 4]) # None; s is set([1, 2, 3, 4])
</pre>
<p>If <code>s</code> and <code>t</code> are sets, <code>s.update(t)</code> is equivalent to
<code>s |= t</code>; however, note that the <code>|=</code> augmented assignment requires both sides
to be sets, while the <code>update</code> method also accepts sequences and dicts.
<p>It is permissible to call <code>update</code> without any arguments; this leaves the set
unchanged.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="update.others">
<code>others</code>
</td>
<td>
required<br/>
Sets, sequences of hashable elements, or dicts.
</td>
</tr>
</tbody>
</table>
</body>
</html>
<!-- {% endraw %} -->