Sets ({1, 2, 3})
Sets in Luma are unordered collections of unique values. They’re the right choice when you need to track membership, eliminate duplicates, or perform operations like union and intersection.
Declaration
Sets use curly braces with values (no key-value pairs):
numbers: {int} = {1, 2, 3, 4, 5}
names: {str} = {"Alice", "Bob", "Charlie"}
scores: {float} = {9.5, 8.0, 7.5}Supported element types: int, float, str.
An empty {} defaults to an empty map. Use a type annotation to create an empty set:
empty: {int} = {}Quick Reference
| Method | Syntax | Returns | Mutates |
|---|---|---|---|
| Contains | s.contains(v) |
bool |
No |
| Add | s.add(v) |
— | Yes |
| Remove | s.remove(v) |
— | Yes |
| Size | s.size() |
int |
No |
| Union | s.union(other) |
{T} |
No |
| Intersect | s.intersect(other) |
{T} |
No |
| Difference | s.difference(other) |
{T} |
No |
| To List | s.to_list() |
[T] |
No |
| Walk | s.walk(v) -> { ... } |
— | No |
Methods
contains
Check if a value is in the set:
numbers: {int} = {1, 2, 3, 4, 5}
print(numbers.contains(3)) // true
print(numbers.contains(99)) // falseadd
Add an element. Duplicates are ignored:
numbers: {int} = {1, 2, 3}
numbers.add(4)
print(numbers) // {1, 2, 3, 4}
numbers.add(3) // already exists, no effect
print(numbers) // {1, 2, 3, 4}remove
Remove an element:
numbers: {int} = {1, 2, 3}
numbers.remove(2)
print(numbers) // {1, 3}size
Return the number of elements:
numbers: {int} = {1, 2, 3, 4, 5}
print(numbers.size()) // 5
empty: {int} = {}
print(empty.size()) // 0union
Return a new set with all elements from both sets:
a: {int} = {1, 2, 3}
b: {int} = {3, 4, 5}
result: {int} = a.union(b) // {1, 2, 3, 4, 5}Neither a nor b is modified.
intersect
Return a new set with elements present in both sets:
a: {int} = {1, 2, 3, 4, 5}
b: {int} = {3, 4, 5, 6, 7}
result: {int} = a.intersect(b) // {3, 4, 5}difference
Return a new set with elements in the first set but not in the second:
a: {int} = {1, 2, 3, 4, 5}
b: {int} = {3, 4, 5, 6, 7}
result: {int} = a.difference(b) // {1, 2}Note: a.difference(b) is not the same as b.difference(a):
reverse: {int} = b.difference(a) // {6, 7}to_list
Convert a set to a list:
names: {str} = {"Alice", "Bob", "Charlie"}
list: [str] = names.to_list()Since sets are unordered, the list order is not guaranteed. Sort after converting if you need a stable order:
list: [str] = names.to_list()
list.sort()Walking a Set
Iterate over set elements with .walk():
names: {str} = {"Alice", "Bob", "Charlie"}
names.walk(n) -> print(n)With a block body:
numbers: {int} = {1, 2, 3}
numbers.walk(n) -> {
print("number: ${n}")
}Sets are unordered — iteration order is not guaranteed.
Use skip to skip an iteration, or stop to exit the walk early:
numbers: {int} = {1, 2, 3, 4, 5}
numbers.walk(n) -> {
if n % 2 == 0 { skip }
print(n)
}See Walk Loops (skip & stop) for more details.
Set Operations
All three set operations (union, intersect, difference) return new sets and leave the originals unchanged:
a: {int} = {1, 2, 3, 4, 5}
b: {int} = {3, 4, 5, 6, 7}
united: {int} = a.union(b) // {1, 2, 3, 4, 5, 6, 7}
common: {int} = a.intersect(b) // {3, 4, 5}
diff: {int} = a.difference(b) // {1, 2}Examples
Deduplication
words: {str} = {"hello", "world", "hello", "luma", "world"}
print(words.size()) // 3 (duplicates removed)Membership check
allowed: {str} = {"admin", "editor", "viewer"}
role: str = "editor"
if allowed.contains(role) {
print("Access granted")
}Combining sets
team_a: {str} = {"Alice", "Bob", "Charlie"}
team_b: {str} = {"Charlie", "Diana", "Eve"}
all_members: {str} = team_a.union(team_b)
overlap: {str} = team_a.intersect(team_b) // {"Charlie"}
only_a: {str} = team_a.difference(team_b) // {"Alice", "Bob"}Notes
- Sets are unordered — don’t rely on iteration order
- Duplicates are automatically removed when creating or adding to a set
- Supported element types are
int,float, andstr union,intersect, anddifferencereturn new sets — they never modify the originals- Use
.to_list()when you need ordering or index access