Skip to content

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

  • range 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
  • push add v to the end of the list, changing it, and return the list
  • pop take the last item off the list, changing it, and return that item, or nil if the list is empty
  • shift take the first item off the list, changing it, and return that item, or nil if the list is empty
  • unshift add v to the front of the list, changing it, and return the list
  • map a new list of f(item) for every item
  • filter keep only the items that pass the test f
  • reduce combine the items into one value
  • flatten replace each list inside the list with its items, one level deep
  • zip pair up items by position: the first of each list, then the second, and so on, stopping when the shortest list runs out
  • enumerate each item paired with its position, as [index, item], counting from 0
  • group_by sort items into groups by what f returns for each
  • count_by count items by what f returns for each
  • chunks split into lists of n items each, where the last one may be shorter
  • windows 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]
  • rotate the list with its items shifted left by k places, the ones that fall off the front coming back at the end
  • step every nth item, starting with the first
1
# a..b counts from a up to just before b
2
1..4[1, 2, 3]
3
# a..=b includes b
4
1..=4[1, 2, 3, 4]
5
# list + list joins them
6
[1] + [2, 3][1, 2, 3]
Example outputCtrl+Enter to run
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

1
range(4)[0, 1, 2, 3]
2
range(2, 5)[2, 3, 4]
3
range(0, 1, 0.25)[0, 0.25, 0.5, 0.75]
4
range(0 m, 1 m, 25 cm)[0 m, 0.25 m, 0.5 m, 0.75 m]
5
range(10, 0, -5)[10, 5]
Example outputCtrl+Enter to run

See also map, step

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]
Example outputCtrl+Enter to run

See also pop, unshift

pop(xs: list) -> any

take the last item off the list, changing it, and return that item, or nil if the list is empty

1
xs = [1, 2, 3]; [xs.pop, xs][3, [1, 2]]
Example outputCtrl+Enter to run

See also push, shift

shift(xs: list) -> any

take the first item off the list, changing it, and return that item, or nil if the list is empty

1
xs = [1, 2, 3]; [xs.shift, xs][1, [2, 3]]
Example outputCtrl+Enter to run

See also unshift, pop

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]
Example outputCtrl+Enter to run

See also shift, push

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]
Example outputCtrl+Enter to run

See also filter, reduce, update

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]
Example outputCtrl+Enter to run

Tags: searching

See also map, reduce, filter_keys

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)16
Example outputCtrl+Enter to run

Tags: aggregation

See also sum, map

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]
Example outputCtrl+Enter to run

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

  • strict an error instead of stopping early when the lists differ in length
1
zip([1, 2, 3], ["a", "b"])[[1, "a"], [2, "b"]]
2
zip(*[[1, 2], [3, 4], [5, 6]])[[1, 3, 5], [2, 4, 6]]
3
zip([1, 2], [3, 4], strict: true)[[1, 3], [2, 4]]
Example outputCtrl+Enter to run

See also 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"]
Example outputCtrl+Enter to run

See also zip

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]}
Example outputCtrl+Enter to run

Tags: aggregation

See also 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}
Example outputCtrl+Enter to run

See also group_by, count

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]]
Example outputCtrl+Enter to run

See also windows, flatten

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]
Example outputCtrl+Enter to run

See also chunks

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]
Example outputCtrl+Enter to run

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]
Example outputCtrl+Enter to run

See also range

1
# every third number
2
(1..=10).step(3)[1, 4, 7, 10]
3
# primes under 100
4
(1..100).filter(is_prime).len25
5
# the sum of the first 10 squares
6
(1..=10).map(|x| x ** 2) |> sum385
7
# combine items one at a time, starting from 10
8
[1, 2, 3].reduce(10, |acc, x| acc + x)16
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.