> For the complete documentation index, see [llms.txt](https://docs.airops.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.airops.com/actions/workflow-concepts/workflow-steps/flow/iteration.md).

# Iteration

Execute a step multiple times over a list of objects

## How does the Iteration Step work?

The Iteration Step executes the same step(s) over each entry within a list. The only prerequisite to using an Iteration Step is ensuring that your input values have already been formatted into a list object.

<figure><img src="https://3762890407-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX2n5yPRPynbnWuO4SH0M%2Fuploads%2Fgit-blob-5908d89cc09763f9eaca59287abd7d1e726b3854%2Fiteration-step-01-loop.png?alt=media" alt="Ranking Page Analyzer Workflow with an Iteration step that wraps a Prompt LLM step"><figcaption><p>The Iteration step runs the steps inside it once per list item.</p></figcaption></figure>

## What is a list?

A list is a specific data type with elements separated by commas that begins with `[` and ends with `]` . For example:

**A list of numbers:**

```
[1,2,3,4,5]
```

**A list of strings:**

```
["sales", "marketing", "finance"]
```

**A list of JSON objects:**

```
[
  {
    "link": "https://openai.com/",
    "title": "OpenAI",
    "source": "OpenAI"
  },
  {
    "link": "https://www.youtube.com/@OpenAI",
    "title": "OpenAI",
    "source": "YouTube"
  },
  {
    "link": "https://en.wikipedia.org/wiki/OpenAI",
    "title": "OpenAI",
    "source": "Wikipedia"
  }
]
```

## How do I create a list?

Lists can be generated in multiple different ways, including through an LLM step, code step, Google Search or API step. It ultimately comes down to what works best for your use-case, and we've provide a few examples below to help.

### Creating a list via an LLM Step

Because our LLM Steps allow you to adjust the prompt until you've achieved the desired outcome, it's a simple matter of specifying the list format within your prompt.

Let's walk through an example workflow that takes in a country name as input, and returns the top 5 cities in that country. We'll adjust the existing workflow to return those top 5 cities in a list format:

### Creating a list via a Code Step

As our [Code Step](https://docs.airops.com/actions/workflow-concepts/workflow-steps/logic-steps/code.md) allows you to utilize both Javascript and Python, you can hard-code your return statement to match the desired list format, e.g.

```javascript
return(["San Francisco", "New York City", "Miami"]);
```

### Generating a list via a Google Search Step

Our Google Search Step allows you to select the specific formatting of your resulting search:

<figure><img src="https://3762890407-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX2n5yPRPynbnWuO4SH0M%2Fuploads%2Fgit-blob-1cca0e01cb9c8a0672dce25b2e13e8dc82512a6f%2Fiteration-step-02-links-list.png?alt=media" alt="Google Search step Find ranking pages with the result type set to Links Only (List)"><figcaption><p>Choose Links Only (List) to get a list for the Iteration step.</p></figcaption></figure>

The resulting output of the Google Search Step will be formatted like so:

```javascript
// Output of the Google Search Step with Links Only
[
  "https://www.mckinsey.com/featured-insights/mckinsey-explainers/what-is-prompt-engineering",
  "https://www.zdnet.com/article/six-skills-you-need-to-become-an-ai-prompt-engineer/",
  "https://www.techtarget.com/searchenterpriseai/definition/AI-prompt-engineer",
  "https://en.wikipedia.org/wiki/Prompt_engineering",
  "https://www.datacamp.com/blog/what-is-prompt-engineering-the-future-of-ai-communication",
  "https://www.iit.edu/blog/unlock-career-opportunities-ai-how-become-ai-prompt-engineer",
  "https://aws.amazon.com/what-is/prompt-engineering/",
  "https://hbr.org/2023/06/ai-prompt-engineering-isnt-the-future",
  "https://time.com/6272103/ai-prompt-engineer-job/",
  "https://www.promptingguide.ai/"
]
```

## What is the element and element index?

An element is the actual object/value (e.g. string, number, or object) in your list.

The element index is the position of the element within the list, starting with the first element at index 0.

<figure><img src="https://3762890407-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX2n5yPRPynbnWuO4SH0M%2Fuploads%2Fgit-blob-a24a17379343efce73a640eaa9cbcd59ce79cf2f%2Fiteration-step-04-element-variables.png?alt=media" alt="Variables Menu opened from the Prompt LLM step inside an Iteration, with the Iterate through each department step expanded to Output, Element, and Element_index"><figcaption><p>Reference the Iteration's current Element and Element_index from the Variables Menu.</p></figcaption></figure>

## How do I reference the element?

You can reference the element by clicking the pill `step_x.element` within your Iteration Step:

<figure><img src="https://3762890407-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FX2n5yPRPynbnWuO4SH0M%2Fuploads%2Fgit-blob-0aebdf05490a74e8c5ee74f69c0fa1907fc39d44%2Fiteration-step-03-element.png?alt=media" alt="Prompt LLM step inside an Iteration step, with a user message that references the current element from step_1.element"><figcaption><p>Reference the current element with step_x.element.</p></figcaption></figure>

In the example above, the LLM step will run 3 times, and the `{{step_1.element}]` will be replaced with `sales` , `marketing` , and `finance` as such:

* `Give me 3 personas in the sales department`
* `Give me 3 personas in the marketing department`
* `Give me 3 personas in the finance department`

## Is the element index required?

The element index is optional, but it can be used to perform powerful logic.

For example, in the case of our SEO blog writer, we use the `element_index` to help us write our introductions. Because our goal is to have the writer *only write to the first section* of the blog, the element index is useful for only applying the step in that paragraph.

So, we add a [**Conditional** **Step**](https://docs.airops.com/actions/workflow-concepts/workflow-steps/flow/conditional.md) with:

* `step_x.element_index == 0`

which allows us to use a separate LLM Step with the introduction for the first section of the blog only.

## What does the output look like?

The iteration step will also output a list.

The output of each iteration will be added to the list in the same order of the input.

## How do I turn the list into text?

You can turn the array back into text with a simple **Text** step that uses Liquid:

```liquid
// Replace step_x.output

{% for chunk in step_x.output %}
{{chunk}}
{% endfor %}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.airops.com/actions/workflow-concepts/workflow-steps/flow/iteration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
