set#
set is a mutable, unordered collection of unique hashable
values. in and add / remove are O(1) average. The right
container for membership tests, deduplication, and set algebra.
Daily kit on set. Examples assume xs = {1, 2, 3}.
Operation |
Example |
Effect |
|---|---|---|
Literal |
|
Standard set literal. |
From iterable |
|
|
Empty |
|
Empty set; |
|
|
Insert (no-op if already present). |
|
|
Remove if present; silent on miss. |
|
|
Remove; |
|
|
Remove and return an arbitrary element. |
Set algebra with the bitwise operators. Examples assume
a = {1, 2, 3} and b = {2, 3, 4}.
Operation |
Example |
Result |
|---|---|---|
Union |
|
|
Intersection |
|
|
Difference |
|
|
Symmetric difference |
|
|
Subset |
|
|
Superset |
|
|
Idiomatic uses, one per block.
Membership against a small fixed set, cleaner than chained
==.
if x in {1, 2, 3}:
...
De-duplicate any iterable.
uniques = set(xs)
Find required keys absent from a presence set.
missing = required - present
Only hashable values fit in a set. list and dict are not
hashable; use tuple and frozenset when nesting is needed.
Method catalog#
The full instance-method surface on set. Every set-algebra
method also has an operator form; the methods accept any
iterable, the operators require a set on both sides.
Mutators#
Method |
Effect |
|---|---|
|
Insert (no-op if already present). |
|
Remove if present; silent on miss. |
|
Remove; |
|
Remove and return an arbitrary element. |
|
Drop every element. |
|
In-place union with iterables. |
|
In-place intersection. |
|
In-place difference. |
|
In-place symmetric difference. |
Set algebra (return a new set)#
Method |
Equivalent |
|---|---|
|
|
|
|
|
|
|
|
|
Shallow copy. |
Predicates#
Method |
|
|---|---|
|
No common elements. |
|
Every element is in |
|
Every element of |
add and discard mutate the set in place.
xs = {1, 2}; xs.add(3); xs.discard(99); xs
{1, 2, 3}
update is in-place union with any iterable.
xs = {1, 2}; xs.update([2, 3, 4]); xs
{1, 2, 3, 4}
isdisjoint is the cheap “no overlap?” check.
{1, 2}.isdisjoint({3, 4}), {1, 2}.isdisjoint({2, 3})
(True, False)
issubset mirrors the <= operator.
{1, 2}.issubset({1, 2, 3})
True
Operator overloading#
set implements the set-algebra dunders.
Operator |
Dunder |
Returns |
|---|---|---|
|
|
Union. |
|
|
Intersection. |
|
|
Difference. |
|
|
Symmetric difference. |
|
|
In-place union. |
|
|
In-place intersection. |
|
|
In-place difference. |
|
|
In-place symmetric difference. |
|
|
Subset test. |
|
|
Proper subset test. |
|
|
Equality. |
|
|
Membership. |
|
|
Cardinality. |
|
|
Iteration (unordered). |
|
|
|
Set algebra reads naturally with the bitwise operators.
{1, 2, 3} | {3, 4}, {1, 2, 3} & {3, 4}, {1, 2, 3} - {3, 4}
({1, 2, 3, 4}, {3}, {1, 2})
A class that wraps a set can forward __or__ /
__contains__ to behave like a set in expressions.
class TagSet:
def __init__(self, tags=()): self.tags = set(tags)
def __or__(self, other): return TagSet(self.tags | other.tags)
def __contains__(self, t): return t in self.tags
def __repr__(self): return f"TagSet({self.tags})"
"admin" in TagSet({"user", "admin"})
True