zip
Pairs values from two lists and returns a list of pairs.
The input parameters can take lists or list-like objects such as table columns or JSON arrays. It pairs values by their index number, so the Index 0 value for LIST 1 is paired with the Index 0 value for LIST 2, and so on.
The output is a list of pairs — that is, a list of lists.
Parameters
LIST 1 (list, column, row, or array)
The first list or list-like object.
LIST 2 (list, column, row, or array)
The second list or list-like object.
Examples
ATL in Script | Resulting List | Printed Result |
|---|---|---|
[[
list1 = makeList('A','B','C')
list2 = makeList(1, 2, 3)
zip(list1, list2)
]] | ( (A, 1), (B, 2), (C, 3) ) | A and 1, B and 2 and C and 3 |
When you input lists of unequal length, the function stops at end of the shorter list.
ATL in Script | Resulting List | Printed Result |
|---|---|---|
[[
list1 = makeList('A','B','C')
list2 = makeList(1, 2, 3, 4)
zip(list1, list2)
]] | ( (A, 1), (B, 2), (C, 3) ) | A and 1, B and 2 and C and 3 |
Note
There is no warning when you input lists of unequal length.
Using zip with table data
Assume a "Describe the Table" project with this data:
Code | Branch | Manager | Sales | |
|---|---|---|---|---|
Row 1 | 1001 | Aberdeen | Andrew Gray | 198248.36 |
Row 2 | 1002 | Glasgow | Emma Moore | 267321.77 |
Row 3 | 1003 | Edinburgh | Linda Barclay | 182434.58 |
A column variable is a list-like object and therefore a valid input to the function.
To pair values from the Branch and Sales columns:
ATL in Script | Resulting List |
|---|---|
| ( (Aberdeen, 198248.36), (Glasgow, 267321.77), (Edinburgh, 182434.58) ) |
Note
In a "Describe Row in Context" project, the column variables are BranchColumn and SalesColumn.
Here's how you could use the result to produce narrative text:
[[global.results = zip(Branch, Sales);'']] [[map(results, x -> "[[x[0]]] achieved sales of [[currencyFormat(x[1],'GBP')]]")]]
The output text is:
Aberdeen achieved sales of £198,248.36, Glasgow achieved sales of £267,321.77 and Edinburgh achieved sales of £182,434.58.
Note that you could achieve the same result more effiiciently with the pairwiseMap function:
[[pairwiseMap(Branch, Sales, (x,y) -> "[[x]] achieved sales of [[currencyFormat(y,'GBP')]]")]].
Using zip with JSON data
Assume a "Describe a JSON Object" project with this data:
{
"yr2021": {
"Q1": [
{"product": "Retine", "units": 232, "sales": 2493146.89},
{"product": "Ointmo", "units": 314, "sales": 3486198.56},
{"product": "Glower", "units": 174, "sales": 1756934.95}
],
"Q2":[
{"product": "Retine", "units": 274, "sales": 2788146.22},
{"product": "Ointmo", "units": 294, "sales": 3027634.12},
{"product": "Glower", "units": 181, "sales": 1831749.94}
]
}
}Here's how you could create LIST variables and input them to the zip function:
ATL in Script | Resulting List |
|---|---|
[[ Q1array = WholeJSON.yr2021.Q1 productsQ1 = map(Q1array, x -> x.product) unitsSoldQ1 = map(Q1array, x -> x.units) Q1results = zip(productsQ1, unitsSoldQ1) ]] | ( (Retine, 232), (Ointmo, 314), (Glower, 174) ) |
Here's how you might use the result to produce narrative text:
[[ Q1array = WholeJSON.yr2021.Q1 productsQ1 = map(Q1array, x -> x.product) unitsSoldQ1 = map(Q1array, x -> x.units) Q1results = zip(productsQ1, unitsSoldQ1) "In Quarter 1, we sold [[map(Q1results, x -> "[[x[1]]] units of [[x[0]]]")]]." ]]
The output text is:
In Quarter 1, we sold 232 units of Retine, 314 units of Ointmo and 174 units of Glower.
Here is a more complex example using the same data:
[[
products = map(WholeJSON.yr2021.Q1, x -> x.product)
Q1sales = map(WholeJSON.yr2021.Q1, x -> x.sales)
Q2sales = map(WholeJSON.yr2021.Q2, x -> x.sales)
salesResults = zip(Q1sales, Q2sales)
changeValues = map(salesResults, x -> abbreviateNumber(currencyFormat(diff(x[1], x[0]))))
changeResults = zip(products, changeValues)
"From Q1 to Q2, [[map(changeResults, x -> "[[x[0]]] sales [[if(x[1] > 0){increased}else{decreased}]] by [[abs(x[1])]]")]]."
]]
The output text is:
From Q1 to Q2, Retine sales increased by $295K, Ointmo sales decreased by $458.6K and Glower sales increased by $74.8K.