Sets
A set is a collection of unique values. A value is either in the set or not; it can’t be there twice.
numbers: {int} = {1, 2, 3, 4, 5}
names: {str} = {"Alice", "Bob", "Charlie"}
print(numbers)
print(names)Output:
{1, 2, 3, 4, 5}
{"Alice", "Bob", "Charlie"}Use a set when what matters is whether something is included: visited pages, unique tags, allowed users. Use a list when order or duplicates matter. For example, a list of scores keeps two equal scores, while a set would keep only one.
Set types
The type of a set is its element type in curly braces:
| Type | A set of |
|---|---|
{int} | ints |
{str} | strings |
{byte} | bytes |
{bool} | bools |
Elements can be int, byte, bool or str, the same types that can be map keys:
floatcan’t be an element, becausenanis not equal to itself, so it could never be found again.- Lists, maps and sets can’t be elements, because they can change after being added.
An empty set {} gets its type from where it is used, so it must be declared first. s: {int} = {} is an empty set, and m: {str: int} = {} is an empty map.
A set literal can’t contain the same value twice when the values are written directly: {1, 2, 1} is a compile error.
Adding
set[] = value adds a value. Adding a value that is already in the set changes nothing:
names: {str} = {"Alice", "Bob"}
names[] = "Dave"
names[] = "Alice"
print(names)
print(len(names))Output:
{"Alice", "Bob", "Dave"}
3Checking
has(set, value) is true if the value is in the set:
admins: {str} = {"Ada", "Linus"}
print(has(admins, "Ada"))
print(has(admins, "Bob"))Output:
true
falseSets have no positions, so names[0] is an error. Use has instead.
Removing
remove(set, value) removes the value. Removing a value that is not in the set stops the program with a runtime error: runtime error: element "Bob" not found.
names: {str} = {"Alice", "Bob"}
remove(names, "Bob")
print(names)Output:
{"Alice"}Size
len(set) gives the number of elements.
Union, intersection, difference
These three built-in functions each take two sets of the same type and give a new set; the two sets themselves are not changed.
| Function | Gives the elements that are |
|---|---|
a.union(b) | in a or in b (or both) |
a.intersect(b) | in both a and b |
a.diff(b) | in a but not in b |
a: {int} = {1, 2, 3, 4}
b: {int} = {3, 4, 5, 6}
print(a.union(b))
print(a.intersect(b))
print(a.diff(b))
print(b.diff(a))Output:
{1, 2, 3, 4, 5, 6}
{3, 4}
{1, 2}
{5, 6}They can be combined: a.diff(b).union(b.diff(a)) gives the elements that are in exactly one of the two sets.
Order
What matters in a set is which values it contains, not their order. Still, Luma always shows the elements in a fixed order, the order in which they were added, so a program prints the same thing every time it runs:
- An element that is removed and added again goes to the end.
a.union(b)lists the elements ofafirst, then the new ones fromb.intersectanddiffkeep the order ofa.
Sets inside maps and lists
tags: {str: {str}} = {"go": {"fast", "simple"}}
tags["go"][] = "compiled"
print(tags)Output:
{"go": {"fast", "simple", "compiled"}}Assignment shares the set
As with lists and maps, assigning a set to another variable doesn’t copy it: both names refer to the same set. To get an independent copy, use s.union({}).
Going through all elements
ids.walk(id) { ... } runs a block for each element, in the order they were added. See Walk loops.
Not available yet
- Sets can’t be compared with
==.
See Limitations.