Reference · Lists

zip

signature
zip(list | null, list | null) -> list

What it does

Pairs the two lists item by item: a list of [a, b] lists, the first of each, then the second, and so on, as long as the shorter list. null counts as an empty list.

Examples

print(zip([1, 2, 3], ["a", "b", "c"]))let names = ["ann", "bob"]let ages = [30, 40]for [name, age] in zip(names, ages) {    print(name, age)}print(zip(names, ages) | map((p) => { name: p[0], age: p[1] }))
output
[[1, "a"], [2, "b"], [3, "c"]]ann 30bob 40[{ name: "ann", age: 30 }, { name: "bob", age: 40 }]

Edge cases

print(zip([1, 2, 3], ["a"]), zip([], [1]), zip(null, [1]), zip([1], null))print(zip(["a", "b"], range(2)), enumerate(["a", "b"]))
output
[[1, "a"]] [] [] [][["a", 0], ["b", 1]] [[0, "a"], [1, "b"]]

Errors

An argument that is not a list or null is a runtime error, not an error value.

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

Chaining

In a pipeline the first list comes first and the second is the only argument: xs | zip(ys). The pairs go on to a map that combines them.

print([1, 2, 3] | zip([10, 20, 30]) | map((p) => p[0] + p[1]))print(["x", "y"] | zip([1, 2]) | map((p) => `${p[0]}=${p[1]}`) | join("&"))
output
[11, 22, 33]x=1&y=2

See also

enumerate, map, flatten, range

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

Thread 0 posts

On zip(): 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