Iterators

Iteration, often referred to as looping or batch processing, means to repeat a process over and over with some degree of automation. Iteration is very important because automating repetitive tasks reduces the time and effort required to perform the tasks. With iteration in ModelBuilder, a process can be executed over and over using different settings or data in each iteration. ModelBuilder also provides flexibility in iteration, as an entire model or a single tool or process can be executed repeatedly.

Note:
  • Only one iterator can be used per model. The button to add another iterator will be disabled if one iterator exists in the model.
  • If an iterator is added to a model, all tools in the model iterate for each value in the iterator. If you do not want to run each tool in the model for each iterated value, create another model and add only the iterator and processes that you want to run multiple times, save it, and add it as a model tool into the main model. Learn about adding a submodel to a model.
  • To give a unique name to the output of any tool connected to an iterator, use inline variable substitution.

There are a number of iterators in ModelBuilder that you can use for iterating over different values, datasets, and workspaces.

IteratorDescription

For

Iterates over a starting and ending value by a given value.

Iterate Datasets

Iterates over datasets in a workspace or feature dataset.

Iterate Feature Classes

Iterates over feature classes in a workspace or feature dataset.

Iterate Feature Selection

Iterates over features in a feature class.

Iterate Field Values

Iterates over each value in a field.

Iterate Fields

Iterates fields in a table.

Iterate Files

Iterates over files in a folder.

Iterate Multivalue

Iterates over a list of values.

Iterate Layers

Iterates layers in a map.

Iterate Rasters

Iterates over rasters in a workspace.

Iterate Row Selection

Iterates over rows in a table.

Iterate Tables

Iterates over tables in a workspace.

Iterate Workspaces

Iterates over workspaces in a folder.

While

Iterates until a condition becomes true or until a condition comes false.

Add and use an iterator

To add an iterator to your model, click ModelBuilder > Insert > Iterator and choose from the list of iterators. Only one iterator can be used per model. The options to add another iterator will be disabled if one iterator exists in the model.

Double-click the iterator element to view and change its settings. Each of the iterators are similar, though each iterator may have settings that differ from the other iterators. A commonly used iterator, Iterate Feature Classes, is explained below.

Iterate Feature Classes

ElementDescription
Workspace

Input workspace (such as a geodatabase) or a feature dataset.

Wildcard

Combination of * and characters that help to limit the results. For example, A* means that only feature classes starting with A are included. This is optional.

Feature Type

A feature type filter that helps limit the results. The most common are Point, Line, and Polygon. This is optional.

Recursive

Determines whether the iterator will go into subfolders in the workspace. This is optional.

Output Feature Class

Output variable that contains the path of the feature class. This variable can be used as input to all geoprocessing tools that accept feature layers or feature classes.

Name

Output variable that contains the name of the feature class. This is a string.

Iterate Feature Classes requires an Input Workspace (the variable named Workspace in the graphic above) where all the feature classes to iterate through are stored. Three additional settings, Wildcard, Feature Type, and Recursive, are used to restrict which feature classes in the workspace are iterated and control the iteration in subfolders in the workspace.

Iterate Feature Classes has two output variables: a feature class and the name of the feature class. The feature class variable can be connected to the next tool for processing, and the Name variable can be used for inline variable substitution.

Example

The example below uses the Iterate Feature Classes iterator to project every feature class in a workspace to a new map projection. Each new feature class created by the Project tool is named based on the input Name using inline variable substitution. In addition, a new field is added to each output feature class. After adding the Iterate Feature Classes iterator to ModelBuilder, follow these general steps:

  1. Specify an input workspace, such as a geodatabase, to iterate through every feature class in that workspace.
  2. Specify a wildcard and feature type (in the example, only polygon feature classes with a name starting with the letter I will be included).
  3. Iterate Feature Classes produces two outputs: the feature class (Output Feature Class) and the name of the feature class (Name). Connect the Output Feature Class variable to the Project tool to use it as input, and use the Name variable and inline variable substitution to name each output from the Project tool with the same name as the input feature class. The Project tool output is written into another geodatabase.
  4. Connect the output of the Project tool to the Add Field tool to add a new field.
Iterator example