Reference · Lists

enumerate

signature
enumerate(list | null) -> list

What it does

A list of pairs [index, item], one for each item, the index counting from 0. It gives a loop or a map the position along with the item. null counts as an empty list.

Examples

print(enumerate(["a", "b", "c"]))for [i, name] in enumerate(["ann", "bob"]) {    print(i, name)}print(enumerate(["a", "b"]) | map((p) => `${p[0] + 1}. ${p[1]}`))
output
[[0, "a"], [1, "b"], [2, "c"]]0 ann1 bob["1. a", "2. b"]

Edge cases

print(enumerate([]), enumerate(null))print(enumerate(["x"]) | map((p) => { let [i, x] = p    `${i}=${x}` }))print(enumerate([null]))
output
[] []["0=x"][[0, null]]

Errors

An argument that is not a list or null is a runtime error, not an error value. Split a string first to number its characters.

this one fails
print(enumerate("ab"))
output
line 1: `enumerate` expects a list as argument 1, got str

Chaining

In a pipeline enumerate takes the list and needs no arguments; the pairs go on to a map or a filter that reads the index.

print(["a", "b", "c"] | enumerate | filter((p) => p[0] % 2 == 0) | map((p) => p[1]))print(["a", "b"] | enumerate | map((p) => `${p[0]}:${p[1]}`) | join(" "))print(split("abc", "") | enumerate)
output
["a", "c"]0:a 1:b[[0, "a"], [1, "b"], [2, "c"]]

See also

range, zip, map, index_of

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On enumerate(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo