---
title: "aiken/collection/list"
---

> Documentation Index
> Fetch the complete documentation index at: https://aiken-lang.com/llms.txt
> Use this file to discover all available pages before exploring further.

# aiken/collection/list

## Types

### `Fold2<a, b, result>` {#Fold2}

A function callback to accumulate two returned values at once. See also [`foldl2`](#foldl2) and [`foldr2`](#foldr2) about usage.

#### Alias

```aiken
Fold2<a, b, result> = fn(a, b) -> result
```

## Functions

### Constructing

### `push` {#push}

```aiken
push(self: List<a>, elem: a) -> List<a>
```

Add an element in front of the list. Sometimes useful when combined with
other functions.

```aiken
list.push([2, 3], 1) == [1, ..[2, 3]] == [1, 2, 3]
```

### `range` {#range}

```aiken
range(from: Int, to: Int) -> List<Int>
```

Construct a list of a integer from a given range.

```aiken
list.range(0, 3) == [0, 1, 2, 3]
list.range(-1, 1) == [-1, 0, 1]
```

### `repeat` {#repeat}

```aiken
repeat(elem: a, n_times: Int) -> List<a>
```

Construct a list filled with n copies of a value.

```aiken
list.repeat("na", 3) == ["na", "na", "na"]
```

### Inspecting

### `all` {#all}

```aiken
all(self: List<a>, predicate: fn(a) -> Bool) -> Bool
```

Determine if all elements of the list satisfy the given predicate.

:::note
An empty list always satisfies the predicate.
:::

```aiken
list.all([], fn(n) { n > 0 }) == True
list.all([1, 2, 3], fn(n) { n > 0 }) == True
list.all([1, 2, 3], fn(n) { n == 2 }) == False
```

### `any` {#any}

```aiken
any(self: List<a>, predicate: fn(a) -> Bool) -> Bool
```

Determine if at least one element of the list satisfies the given predicate.

:::note
An empty list NEVER satisfies the predicate.
:::

```aiken
list.any([], fn(n) { n > 2 }) == False
list.any([1, 2, 3], fn(n) { n > 0 }) == True
list.any([1, 2, 3], fn(n) { n == 2 }) == True
list.any([1, 2, 3], fn(n) { n < 0 }) == False
```

### `at` {#at}

```aiken
at(self: List<a>, index: Int) -> Option<a>
```

Return `Some(item)` at the index or None if the index is out of range. The index is 0-based.

```aiken
list.at([1, 2, 3], 1) == Some(2)
list.at([1, 2, 3], 42) == None
```

### `count` {#count}

```aiken
count(self: List<a>, predicate: fn(a) -> Bool) -> Int
```

Count how many items in the list satisfy the given predicate.

```aiken
list.count([], fn(a) { a > 2}) == 0
list.count([1, 2, 3], fn(a) { n > 0 }) == 3
list.count([1, 2, 3], fn(a) { n >= 2 }) == 2
list.count([1, 2, 3], fn(a) { n > 5 }) == 0
```

### `find` {#find}

```aiken
find(self: List<a>, predicate: fn(a) -> Bool) -> Option<a>
```

Find the first element satisfying the given predicate, if any.

```aiken
list.find([1, 2, 3], fn(x) { x == 2 }) == Some(2)
list.find([4, 5, 6], fn(x) { x == 2 }) == None
```

### `find_map` {#find_map}

```aiken
find_map(self: List<a>, predicate: fn(a) -> Option<result>) -> Option<result>
```

Like [`find`](#find), but maps the found element to something else. This
provides an efficient way to access and transform an element in a list.

```aiken
expect Some(2) = list.find_map(["a", "bb", "ccc"], fn(x) {
  let len = bytearray.length(x)
  if len > 1 {
    Some(len)
  } else {
    None
  }
})
```

### `has` {#has}

```aiken
has(self: List<a>, elem: a) -> Bool
```

Figures out whether a list contain the given element.

```aiken
list.has([1, 2, 3], 2) == True
list.has([1, 2, 3], 14) == False
list.has([], 14) == False
```

### `head` {#head}

```aiken
head(self: List<a>) -> Option<a>
```

Get the first element of a list

```aiken
list.head([1, 2, 3]) == Some(1)
list.head([]) == None
```

### `is_empty` {#is_empty}

```aiken
is_empty(self: List<a>) -> Bool
```

Checks whether a list is empty.

```aiken
list.is_empty([]) == True
list.is_empty([1, 2, 3]) == False
```

### `index_of` {#index_of}

```aiken
index_of(self: List<a>, elem: a) -> Option<Int>
```

Gets the index of an element of a list, if any. Otherwise, returns None.

```aiken
list.index_of([1, 5, 2], 2) == Some(2)
list.index_of([1, 7, 3], 4) == None
list.index_of([1, 0, 9, 6], 6) == Some(3)
list.index_of([], 6) == None
```

### `last` {#last}

```aiken
last(self: List<a>) -> Option<a>
```

Get the last in the given list, if any.

```aiken
list.last([]) == None
list.last([1, 2, 3]) == Some(3)
```

### `length` {#length}

```aiken
length(self: List<a>) -> Int
```

Get the number of elements in the given list.

```aiken
list.length([]) == 0
list.length([1, 2, 3]) == 3
```

### Inspecting Forcibly

### `expect_any` {#expect_any}

```aiken
expect_any(self: List<a>, predicate: fn(a) -> Bool) -> Void
```

More efficient version of [`any`](#any), but fails if no element satisfies the predicate.

:::note
An empty list NEVER satisfies the predicate.
:::

```aiken
list.expect_any([], fn(n) { n > 2 }) → 💥
list.expect_any([1, 2, 3], fn(n) { n > 0 }) == True
list.expect_any([1, 2, 3], fn(n) { n == 2 }) == True
list.expect_any([1, 2, 3], fn(n) { n < 0 }) → 💥
```

### `expect_at` {#expect_at}

```aiken
expect_at(self: List<a>, index: Int) -> a
```

More efficient version of [`at`](#at), but fails if there's no element at the
given index (or if the index is out of bound).

```aiken
list.expect_at([1, 2, 3], 1) == 2
list.expect_at([1, 2, 3], 42) → 💥
```

### `expect_find` {#expect_find}

```aiken
expect_find(self: List<a>, predicate: fn(a) -> Bool) -> a
```

More efficient version of [`find`](#find), but fails if the element is not found.

```aiken
list.expect_find([1, 2, 3], fn(x) { x == 2 }) == 2
list.expect_find([4, 5, 6], fn(x) { x == 2 }) → 💥
```

### `expect_find_map` {#expect_find_map}

```aiken
expect_find_map(self: List<a>, select: fn(a, fn(b) -> b, fn() -> b) -> b) -> b
```

More efficient version of [`find_map`](#find_map) but fails if no element
is selected.

```aiken
expect 2 = list.expect_find_map(["a", "bb", "ccc"], fn(x, keep, discard) {
  let len = bytearray.length(x)
  if len > 1 {
    keep(len)
  } else {
    discard()
  }
})
```

### `expect_has` {#expect_has}

```aiken
expect_has(self: List<a>, elem: a) -> Void
```

More efficient version of [`has`](#has) but fails if the element is missing.

```aiken
list.expect_has([1, 2, 3], 2) → ✅
list.expect_has([1, 2, 3], 14) → 💥
list.expect_has([], 14) → 💥
```

### `expect_head` {#expect_head}

```aiken
expect_head(self: List<a>) -> a
```

More efficient version of [`head`](#head), but fails if the list is empty.

```aiken
list.expect_head([1, 2, 3]) == 1
list.expect_head([]) → 💥
```

### `expect_index_of` {#expect_index_of}

```aiken
expect_index_of(self: List<a>, elem: a) -> Int
```

More efficient version of [`index_of`](#index_of), but fails if the element
is not in the list.

```aiken
list.expect_index_of([1, 5, 2], 2) == 2
list.expect_index_of([1, 7, 3], 4) → 💥
list.expect_index_of([1, 0, 9, 6], 6) == 3
list.expect_index_of([], 6) → 💥
```

### `expect_last` {#expect_last}

```aiken
expect_last(self: List<a>) -> a
```

More efficient version of [`last`](#last), but fails when the list is empty.

```aiken
list.expect_last([1, 2, 3]) == 3
list.expect_last([]) → 💥
```

### Modifying

#### Extracting

### `delete` {#delete}

```aiken
delete(self: List<a>, elem: a) -> List<a>
```

Remove the first occurrence of the given element from the list.

```aiken
list.delete([1, 2, 3, 1], 1) == [2, 3, 1]
list.delete([1, 2, 3], 14) == [1, 2, 3]
```

### `drop` {#drop}

```aiken
drop(self: List<a>, n: Int) -> List<a>
```

Drop the first `n` elements of a list.

```aiken
list.drop([1, 2, 3], 2) == [3]
list.drop([], 42) == []
list.drop([1, 2, 3], 42) == []
```

### `drop_while` {#drop_while}

```aiken
drop_while(self: List<a>, predicate: fn(a) -> Bool) -> List<a>
```

Returns the suffix of the given list after removing all elements that satisfy the predicate.

```aiken
list.drop_while([1, 2, 3], fn(x) { x < 2 }) == [2, 3]
list.drop_while([], fn(x) { x > 2 }) == []
list.drop_while([1, 2, 3], fn(x) { x == 3 }) == [1, 2, 3]
```

### `filter` {#filter}

```aiken
filter(self: List<a>, predicate: fn(a) -> Bool) -> List<a>
```

Produce a list of elements that satisfy a predicate.

```aiken
list.filter([1, 2, 3], fn(x) { x >= 2 }) == [2, 3]
list.filter([], fn(x) { x > 2 }) == []
list.filter([1, 2, 3], fn(x) { x == 3 }) == [3]
```

### `filter_map` {#filter_map}

```aiken
filter_map(self: List<a>, predicate: fn(a) -> Option<b>) -> List<b>
```

Produce a list of transformed elements that satisfy a predicate.

```aiken
let transform = fn(x) { if x % 2 == 0 { None } else { Some(3*x) } }
list.filter_map([1, 2, 3], transform) == [3, 9]
```

### `init` {#init}

```aiken
init(self: List<a>) -> Option<List<a>>
```

Return all elements except the last one.

```aiken
list.init([]) == None
list.init([1, 2, 3]) == Some([1, 2])
```

### `partition` {#partition}

```aiken
partition(self: List<a>, predicate: fn(a) -> Bool) -> (List<a>, List<a>)
```

Returns a tuple with all elements that satisfy the predicate at first
element, and the rest as second element.

```aiken
list.partition([1, 2, 3, 4], fn(x) { x % 2 == 0 }) == ([2, 4], [1, 3])
```

### `slice` {#slice}

```aiken
slice(self: List<a>, from: Int, to: Int) -> List<a>
```

Extract a sublist from the given list using 0-based indexes. Negative
indexes wrap over, so `-1` refers to the last element of the list.

```aiken
list.slice([1, 2, 3, 4, 5, 6], from: 2, to: 4) == [3, 4, 5]
list.slice([1, 2, 3, 4, 5, 6], from: -2, to: -1) == [5, 6]
list.slice([1, 2, 3, 4, 5, 6], from: 1, to: -1) == [2, 3, 4, 5, 6]
```

### `span` {#span}

```aiken
span(self: List<a>, n: Int) -> (List<a>, List<a>)
```

Cut a list in two, such that the first list contains the given number of /
elements and the second list contains the rest.

Fundamentally equivalent to (but more efficient):

```aiken
// span(xs, n) == (take(xs, n), drop(xs, n))
span([1, 2, 3, 4, 5], 3) == ([1, 2, 3], [4, 5])
```

### `tail` {#tail}

```aiken
tail(self: List<a>) -> Option<List<a>>
```

Get elements of a list after the first one, if any.

```aiken
list.tail([]) == None
list.tail([1, 2, 3]) == Some([2, 3])
```

### `take` {#take}

```aiken
take(self: List<a>, n: Int) -> List<a>
```

Get the first `n` elements of a list.

```aiken
list.take([1, 2, 3], 2) == [1, 2]
list.take([1, 2, 3], 14) == [1, 2, 3]
```

### `take_while` {#take_while}

```aiken
take_while(self: List<a>, predicate: fn(a) -> Bool) -> List<a>
```

Returns the longest prefix of the given list where all elements satisfy the predicate.

```aiken
list.take_while([1, 2, 3], fn(x) { x > 2 }) == []
list.take_while([1, 2, 3], fn(x) { x < 2 }) == [1]
```

### `unique` {#unique}

```aiken
unique(self: List<a>) -> List<a>
```

Removes duplicate elements from a list.

```aiken
list.unique([1, 2, 3, 1]) == [1, 2, 3]
```

#### Extracting Forcibly

### `expect_delete` {#expect_delete}

```aiken
expect_delete(self: List<a>, elem: a) -> List<a>
```

More efficient version of [`delete`](#delete), which also fails when the element is NOT in the list.

```aiken
list.expect_delete([1, 2, 3, 1], 1) == [2, 3, 1]
list.expect_delete([1, 2, 3], 14) → 💥
```

### `expect_drop` {#expect_drop}

```aiken
expect_drop(self: List<a>, n: Int) -> List<a>
```

More efficient version of [`drop`](#drop), which fails when there's less than `n` elements in the list.

Yields the list when `n` is negative.

### `expect_init` {#expect_init}

```aiken
expect_init(self: List<a>) -> List<a>
```

More efficient version of [`init`](#init), which fails when the list is empty.

### `expect_tail` {#expect_tail}

```aiken
expect_tail(self: List<a>) -> List<a>
```

More efficient version of [`tail`](#tail), which fails when the list is empty.

### `expect_take` {#expect_take}

```aiken
expect_take(self: List<a>, n: Int) -> List<a>
```

More efficient version of [`take`](#take), which fails when the list contains less element than wanted.

Yields an empty list when `n` is negative.

#### Mapping

### `flat_map` {#flat_map}

```aiken
flat_map(self: List<a>, with: fn(a) -> List<b>) -> List<b>
```

More efficient
Map elements of a list into a new list and flatten the result.

```aiken
list.flat_map([1, 2, 3], fn(a) { [a, 2*a] }) == [1, 2, 2, 4, 3, 6]
```

### `for_each` {#for_each}

```aiken
for_each(self: List<a>, do: fn(a) -> Void) -> Void
```

Perform an action for each element of a list.

```aiken
list.for_each(labels, do: fn(lbl) { trace lbl Void })
```

### `indexed_map` {#indexed_map}

```aiken
indexed_map(self: List<a>, with: fn(Int, a) -> result) -> List<result>
```

List [`map`](#map) but provides the position (0-based) of the elements while iterating.

```aiken
list.indexed_map([1, 2, 3], fn(i, x) { i + x }) == [1, 3, 5]
```

### `map` {#map}

```aiken
map(self: List<a>, with: fn(a) -> result) -> List<result>
```

Apply a function to each element of a list.

```aiken
list.map([1, 2, 3, 4], fn(n) { n + 1 }) == [2, 3, 4, 5]
```

### `map2` {#map2}

```aiken
map2(self: List<a>, bs: List<b>, with: fn(a, b) -> result) -> List<result>
```

Apply a function of two arguments, combining elements from two lists.

:::note
If one list is longer, the extra elements are dropped.
:::

```aiken
list.map2([1, 2, 3], [1, 2], fn(a, b) { a + b }) == [2, 4]
```

### `map3` {#map3}

```aiken
map3(
  self: List<a>,
  bs: List<b>,
  cs: List<c>,
  with: fn(a, b, c) -> result,
) -> List<result>
```

Apply a function of three arguments, combining elements from three lists.

:::note
If one list is longer, the extra elements are dropped.
:::

```aiken
list.map3([1, 2, 3], [1, 2], [1, 2, 3], fn(a, b, c) { a + b + c }) == [3, 6]
```

### `reverse` {#reverse}

```aiken
reverse(self: List<a>) -> List<a>
```

Return the list with its elements in the reserve order.

```aiken
list.reverse([1, 2, 3]) == [3, 2, 1]
```

### `sort` {#sort}

```aiken
sort(self: List<a>, compare: fn(a, a) -> Ordering) -> List<a>
```

Sort a list in ascending order using the given comparison function.

```aiken
use aiken/primitive/int

list.sort([3, 1, 4, 0, 2], int.compare) == [0, 1, 2, 3, 4]
list.sort([1, 2, 3], int.compare) == [1, 2, 3]
```

### `unzip` {#unzip}

```aiken
unzip(self: List<(a, b)>) -> (List<a>, List<b>)
```

Decompose a list of tuples into a tuple of lists.

```
list.unzip([(1, "a"), (2, "b")]) == ([1, 2], ["a", "b"])
```

### Combining

### `concat` {#concat}

```aiken
concat(left: List<a>, right: List<a>) -> List<a>
```

Merge two lists together.

```aiken
list.concat([], []) == []
list.concat([], [1, 2, 3]) == [1, 2, 3]
list.concat([1, 2, 3], [4, 5, 6]) == [1, 2, 3, 4, 5, 6]
```

### `difference` {#difference}

```aiken
difference(self: List<a>, with: List<a>) -> List<a>
```

Remove the first occurrence of each element of the second list from the first one.

```
list.difference(["h", "e", "l", "l", "o"], ["l", "e", "l"]) == ["h", "o"]
list.difference([1, 2, 3, 4, 5], [1, 1, 2]) == [3, 4, 5]
list.difference([1, 2, 3], []) == [1, 2, 3]
```

### `zip` {#zip}

```aiken
zip(self: List<a>, bs: List<b>) -> List<(a, b)>
```

Combine two lists together.

:::note
If one list is longer, the extra elements are dropped.
:::

```aiken
list.zip([1, 2], ["a", "b", "c"]) == [(1, "a"), (2, "b")]
```

### Transforming

### `foldl` {#foldl}

```aiken
foldl(self: List<a>, zero: b, with: fn(a, b) -> b) -> b
```

Reduce a list from left to right.

```aiken
list.foldl([1, 2, 3], 0, fn(n, total) { n + total }) == 6
list.foldl([1, 2, 3], [], fn(x, xs) { [x, ..xs] }) == [3, 2, 1]
```

### `foldl2` {#foldl2}

```aiken
foldl2(
  self: List<elem>,
  zero_a: a,
  zero_b: b,
  with: fn(elem, a, b, Fold2<a, b, result>) -> result,
  return: Fold2<a, b, result>,
) -> result
```

Reduce a list from left to right, while accumulating two results. This is
faster (albeit equivalent) than accumulating both results in a `Pair` or tuple.

:::tip
Using [`backpassing`](https://aiken-lang.com/language-tour/functions/#backpassing-) to ease readability is a good idea.
:::

```aiken
let len, sum <-
   foldl2(
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     0,
     0,
     fn(n, len, sum, return) { return(len + 1, sum + n) },
   )

and {
  len == 10,
  sum == 55,
}
```

### `foldr` {#foldr}

```aiken
foldr(self: List<a>, zero: b, with: fn(a, b) -> b) -> b
```

Reduce a list from right to left.

```aiken
list.foldr([1, 2, 3], 0, fn(n, total) { n + total }) == 6
list.foldr([1, 2, 3], [], fn(x, xs) { [x, ..xs] }) == [1, 2, 3]
```

### `foldr2` {#foldr2}

```aiken
foldr2(
  self: List<elem>,
  zero_a: a,
  zero_b: b,
  with: fn(elem, a, b, Fold2<a, b, result>) -> result,
  return: Fold2<a, b, result>,
) -> result
```

Reduce a list from right to left, while accumulating two results. This is
faster (albeit equivalent) than accumulating both results in a `Pair` or tuple.

:::tip
Using [`backpassing`](https://aiken-lang.com/language-tour/functions/#backpassing-) to ease readability is a good idea.
:::

```aiken
let len, sum <-
   list.foldr2(
     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     0,
     0,
     fn(n, len, sum, return) { return(len + 1, sum + n) },
   )

and {
  len == 10,
  sum == 55,
}
```

### `from_data` {#from_data}

```aiken
from_data(data: Data) -> Option<List<Data>>
```

Upcast a `Data` into a `List<Data>`

### `indexed_foldr` {#indexed_foldr}

```aiken
indexed_foldr(
  self: List<a>,
  zero: result,
  with: fn(Int, a, result) -> result,
) -> result
```

Like [`foldr`](#foldr), but also provides the position (0-based) of the elements when iterating.

```aiken
let group = fn(i, x, xs) { [(i, x), ..xs] }
list.indexed_foldr(["a", "b", "c"], [], group) == [
  (0, "a"),
  (1, "b"),
  (2, "c")
]
```

### `reduce` {#reduce}

```aiken
reduce(self: List<a>, zero: b, with: fn(b, a) -> b) -> b
```

Reduce a list from left to right using the accumulator as left operand.
Said differently, this is [`foldl`](#foldl) with callback arguments swapped.

```aiken
list.reduce([#[1], #[2], #[3]], #[0], bytearray.concat) == #[0, 1, 2, 3]
list.reduce([True, False, True], False, fn(b, a) { or { b, a } }) == True
```

### Transforming Forcibly

### `expect_from_data` {#expect_from_data}

```aiken
expect_from_data(data: Data) -> List<Data>
```

Like [`from_data`](#from_data) but fails if the given Data is not a CBOR array.

Source: https://aiken-lang.com/stdlib/aiken/collection/list/index.mdx
