On this page
set
Prev: round | Up: __builtins__ | Next: setattr
Rel: bool, int, float, complex, str
Ref: docs.python.org — set
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import set
Overview & Signature
set(iterable=(), /)
Build an unordered collection of unique elements.
Common Methods & Attributes
set.add(self, object, /)— Add an element to a set.set.clear(self, /)— Remove all elements from this set.set.copy(self, /)— Return a shallow copy of a set.set.difference(self, /, *others)— Return a new set with elements in the set that are not in the others.set.difference_update(self, /, *others)— Update the set, removing elements found in others.set.discard(self, object, /)— Remove an element from a set if it is a member.set.intersection(self, /, *others)— Return a new set with elements common to the set and all others.set.intersection_update(self, /, *others)— Update the set, keeping only elements found in it and all others.set.isdisjoint(self, other, /)— Return True if two sets have a null intersection.set.issubset(self, other, /)— Report whether another set contains this set.set.issuperset(self, other, /)— Report whether this set contains another set.set.pop(self, /)— Remove and return an arbitrary set element.set.remove(self, object, /)— Remove an element from a set; it must be a member.set.symmetric_difference(self, other, /)— Return a new set with elements in either the set or other but not both.set.symmetric_difference_update(self, other, /)— Update the set, keeping only elements found in either set, but not in both.set.union(self, /, *others)— Return a new set with elements from the set and all others.set.update(self, /, *others)— Update the set, adding elements from all others.
Usage
a, b = {1, 2, 3}, {3, 4, 5}
assert (a | b) == {1, 2, 3, 4, 5}
assert (a & b) == {3}
assert (a - b) == {1, 2}
Gotchas
{}is an emptydict, NOT an emptyset: Always writeset()for an empty set.- Unordered and elements must be hashable: Do not rely on iteration order across runs.
REPL Inspect
help(set)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/round Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/setattr Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/python3 links to this page