for

Runs a block of commands once for each element of an iterable: a list, a dict, or any Python iterable or async iterable that an expression produces.

Fields

FieldTypeDefaultDescription
iterrequireddynamic string or list or objectWhat to iterate over: a list, dict or any Python iterable.
loopVarlist of name or nameNames bound on each iteration. For a tuple of N elements, give N names.
docommand blockCommands executed for each element produced by the iterator.
maxIterationsdynamic string or integerCaps the number of iterations.

Example

"for": {
  "iter": "{ result['properties']['periods'][:8] }",
  "loopVar": "period",
  "do": {
    "var": {
      "lines": "{ lines + [f'In {period['number']} hours: {period['temperature']} and {period['shortForecast']}'] }"
    }
  }
}

Iterating a dict binds the dict's keys. To get key and value together, iterate { d.items() } and give two loop variables:

"for": {
  "iter": "{ prices.items() }",
  "loopVar": ["sku", "price"],
  "do": {
    "progress": "{ f'{sku} costs {price}' }"
  }
}

Rules

  • When an element is a tuple and several loop variables are given, the tuple is unpacked into the variables in order.
  • The loop variables are locals of the function. The loop variables are removed when the loop ends.
  • A for nested inside another for may not reuse the outer loop's variable names.
  • maxIterations stops the loop quietly when the count is reached. Without maxIterations, the max loop iterations setting in Runtime limits applies.
  • The ask and invoke commands are not allowed inside a for block, nor in any function called from a for block.