# Iterate over a list with for_each

> **About this page**
>
> - Kind: How-to guide
> - Summary: Run a block of tasks once for each item in a list, from another task's output or a run input, and inspect each iteration.
> - Last changed: 2026-11-17
> - Page: https://docs.swale.io/how-to-guides/iterate-over-a-list-with-for-each
> - CLI reference for its commands: [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

This guide shows how to use the `for_each` control task to repeat a block of tasks for each item in a list.

## Basic `for_each` task

Add a `for_each` key to a task entry to make it a `for_each` task:

```yaml
name: For each example
compute_type: cpu-4
blocks:
  main:
    tasks:
      list_files:
        name: List files
        uses: my-account/list-task@1.0.0
      process_each:
        name: Process each
        start_on:
          - list_files
        for_each:
          items: ${{tasks.list_files.outputs.files}}
          block: per_file_block

  per_file_block:
    tasks:
      transform:
        name: Transform
        uses: my-account/transform-task@1.0.0
        args:
          file: ${{tasks.process_each.for_each.item}}
          step: ${{tasks.process_each.for_each.index}}
```

`list_files` runs first and produces an array in its `files` output. `process_each` then iterates over that array, running `per_file_block` once for each element.

Inside `per_file_block`, `${{tasks.process_each.for_each.item}}` is the current element and `${{tasks.process_each.for_each.index}}` its zero-based position; see [For Each loop variables](https://docs.swale.io/reference/expression-language.md#for-each-loop-variables).

## Iterate over a run input

If your run input is already a JSON array, you can iterate over it directly. This version keeps `per_file_block` from the example above:

```yaml
inputs:
  file_list: {}

blocks:
  main:
    tasks:
      process_each:
        name: Process each
        for_each:
          items: ${{inputs.file_list}}
          block: per_file_block
```

Pass the input as a JSON array when starting the run:

```bash
swale workflow run start \
  --project-name my-project \
  --workflow-id <workflow-id> \
  --input 'file_list=["file1.csv","file2.csv","file3.csv"]'
```

## Inspect each iteration

After starting a run, open it and expand the `for_each` task's node in its **Workflow Graph**: it shows one group per iteration, labelled with its item. Click a task inside a group to open that iteration's task. [UI representation](https://docs.swale.io/reference/control-flow.md#ui-representation) describes the node.

## Next steps

- [Control flow reference](https://docs.swale.io/reference/control-flow.md)
- [Use conditional logic in workflows](https://docs.swale.io/how-to-guides/use-conditional-logic-in-workflows.md)
- [How control flow works](https://docs.swale.io/explanation/how-control-flow-works.md)
