Reference · Lists
zip
signature
zip(list | null, list | null) -> listWhat 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
- The result is as long as the shorter list; the extra items of the longer one are dropped without an error.
- Either list empty or
nullgives[]. - Each pair is a two-item list:
p[0],p[1], orlet [a, b] = p. - Pairing a list with its indexes is enumerate.
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 strChaining
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=2See also
enumerate, map, flatten, range
Thread
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