data.lists
build lists, change every item, keep some, combine them, and group them
A list is an ordered run of values, written [1, 2, 3]. These functions build lists, change every item, keep some, combine two lists, and sort items into groups. Most work as methods too, so xs.map(f) is map(xs, f).
Tags: collections
rangethe integers from 0 up to n, or from a up to b, not including the end, or with a step the numbers or quantities betweenpushadd v to the end of the list, changing it, and return the listpoptake the last item off the list, changing it, and return that item, or nil if the list is emptyshifttake the first item off the list, changing it, and return that item, or nil if the list is emptyunshiftadd v to the front of the list, changing it, and return the listmapa new list of f(item) for every itemfilterkeep only the items that pass the test freducecombine the items into one valueflattenreplace each list inside the list with its items, one level deepzippair up items by position: the first of each list, then the second, and so on, stopping when the shortest list runs outenumerateeach item paired with its position, as [index, item], counting from 0group_bysort items into groups by what f returns for eachcount_bycount items by what f returns for eachchunkssplit into lists of n items each, where the last one may be shorterwindowsevery run of n items in a row, sliding along one at a time: [1, 2, 3] with 2 gives [1, 2] and [2, 3]rotatethe list with its items shifted left by k places, the ones that fall off the front coming back at the endstepevery nth item, starting with the first
syntax
Section titled “syntax”1# a..b counts from a up to just before b21..4[1, 2, 3]3# a..=b includes b41..=4[1, 2, 3, 4]5# list + list joins them6[1] + [2, 3][1, 2, 3]Functions
Section titled “Functions”range(n: int) -> list / range(a: int, b: int) -> list / range(a: num|quantity, b: num|quantity, step: num|quantity) -> list
the integers from 0 up to n, or from a up to b, not including the end, or with a step the numbers or quantities between
Same as a..b without a step. A negative step counts down, and each item is a plus a whole number of steps, so decimals don’t drift
1range(4)[0, 1, 2, 3]2range(2, 5)[2, 3, 4]3range(0, 1, 0.25)[0, 0.25, 0.5, 0.75]4range(0 m, 1 m, 25 cm)[0 m, 0.25 m, 0.5 m, 0.75 m]5range(10, 0, -5)[10, 5]push(xs: list, v: any) -> list
add v to the end of the list, changing it, and return the list
1[1, 2].push(3)[1, 2, 3]pop(xs: list) -> any
take the last item off the list, changing it, and return that item, or nil if the list is empty
1xs = [1, 2, 3]; [xs.pop, xs][3, [1, 2]]shift(xs: list) -> any
take the first item off the list, changing it, and return that item, or nil if the list is empty
1xs = [1, 2, 3]; [xs.shift, xs][1, [2, 3]]unshift
Section titled “unshift”unshift(xs: list, v: any) -> list
add v to the front of the list, changing it, and return the list
1[2, 3].unshift(1)[1, 2, 3]map(xs: list, f: fn) -> list
a new list of f(item) for every item
1[1, 2, 3].map(|x| x * 10)[10, 20, 30]See also filter, reduce, update
filter
Section titled “filter”filter(xs: list, f: fn) -> list
keep only the items that pass the test f
1(1..10).filter(|x| x % 3 == 0)[3, 6, 9]Tags: searching
See also map, reduce, filter_keys
reduce
Section titled “reduce”reduce(xs: list, init: any, f: fn) -> any
combine the items into one value
Starting from init, f(so_far, item) is called for each item in turn, and what it returns is passed on as so_far to the next
1[1, 2, 3].reduce(10, |acc, x| acc + x)16Tags: aggregation
flatten
Section titled “flatten”flatten(xs: list) -> list
replace each list inside the list with its items, one level deep
1[[1, 2], [3], 4].flatten[1, 2, 3, 4]See also chunks
zip(*lists: list, strict: bool = false) -> list
pair up items by position: the first of each list, then the second, and so on, stopping when the shortest list runs out
zip(*rows) turns rows into columns
strictan error instead of stopping early when the lists differ in length
1zip([1, 2, 3], ["a", "b"])[[1, "a"], [2, "b"]]2zip(*[[1, 2], [3, 4], [5, 6]])[[1, 3, 5], [2, 4, 6]]3zip([1, 2], [3, 4], strict: true)[[1, 3], [2, 4]]See also enumerate
enumerate
Section titled “enumerate”enumerate(xs: list) -> list
each item paired with its position, as [index, item], counting from 0
1["a", "b"].enumerate[[0, "a"], [1, "b"]]2["a", "b"].enumerate.map(|[i, x]| "{i}:{x}")["0:a", "1:b"]See also zip
group_by
Section titled “group_by”group_by(xs: list, f: fn) -> map
sort items into groups by what f returns for each
The result maps each answer to the list of items that gave it
1["apple", "avocado", "banana"].group_by(|w| w[0]){a: ["apple", "avocado"], b: ["banana"]}2(1..=6).group_by(|x| x % 2 == 0){false: [1, 3, 5], true: [2, 4, 6]}Tags: aggregation
See also count_by
count_by
Section titled “count_by”count_by(xs: list, f: fn) -> map
count items by what f returns for each
The result maps each answer to how many items gave it
1"mississippi".chars.count_by(|c| c){m: 1, i: 4, s: 4, p: 2}chunks
Section titled “chunks”chunks(xs: list, n: int) -> list
split into lists of n items each, where the last one may be shorter
1(1..=7).chunks(3)[[1, 2, 3], [4, 5, 6], [7]]windows
Section titled “windows”windows(xs: list, n: int) -> list
every run of n items in a row, sliding along one at a time: [1, 2, 3] with 2 gives [1, 2] and [2, 3]
1[1, 2, 3, 4].windows(2)[[1, 2], [2, 3], [3, 4]]2[1, 4, 9, 16].windows(2).map(|[a, b]| b - a)[3, 5, 7]See also chunks
rotate
Section titled “rotate”rotate(xs: list, k: int) -> list
the list with its items shifted left by k places, the ones that fall off the front coming back at the end
A negative k shifts right
1[1, 2, 3, 4].rotate(1)[2, 3, 4, 1]2[1, 2, 3, 4].rotate(-1)[4, 1, 2, 3]See also reverse
step(xs: list, n: int) -> list
every nth item, starting with the first
1(0..=20).step(5)[0, 5, 10, 15, 20]2(1..10).step(2)[1, 3, 5, 7, 9]See also range
More examples
Section titled “More examples”1# every third number2(1..=10).step(3)[1, 4, 7, 10]3# primes under 1004(1..100).filter(is_prime).len255# the sum of the first 10 squares6(1..=10).map(|x| x ** 2) |> sum3857# combine items one at a time, starting from 108[1, 2, 3].reduce(10, |acc, x| acc + x)16Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.