typestar

Frozen sets in Python

An immutable set, so it can be a dict key or live inside another set.

core = frozenset({"basics", "pythonic"})
extra = frozenset({"pythonic", "domains"})

print(core | extra)
print(core & extra, core - extra)
print(core.issubset(core | extra))

by_group = {core: "stdlib", extra: "mixed"}
print(by_group[frozenset({"pythonic", "basics"})])

How it works

  1. frozenset supports every read operation a set does.
  2. It is hashable, which a set is not.
  3. Set algebra returns frozensets when either side is frozen.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
269
Tokens
88
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 56 seconds.

Type this snippet

Step 1 of 3 in Sets & mappings, step 56 of 72 in Language basics.

← Previous Next →