As you dive deeper into n8n, you’ll quickly realize the power of nodes like Loop for processing lists of data. But what happens when you need to handle more complex scenarios, like iterating over one list of data for every single item in another? This is the realm of nested loops — a fundamental concept in programming that requires a clever workaround in n8n.
In this guide, we’ll walk through a common problem with nested loops in n8n and reveal an elegant solution using sub-workflows.
The Initial Approach: A Tale of Two Loops
Let’s imagine a scenario where you have two distinct lists of data you need to combine: a list of colors and a list of numbers. Your goal is to create a new item for every possible combination of a color and a number.
A natural first instinct is to build a simple nested loop structure. You might start by creating a list of colors, perhaps in a Code node:
Then, you would use a Loop node to iterate through each color.
After executing the workflow, we can see all three colors appear in the Done Branch, which is exactly what we would expect at this stage.
Inside that loop, you’d add another Code node with your numbers:
Finally, you would add a nested Loop node to process the numbers, and a Set node at the very end to combine the data and see the results.
Your workflow might look something like this:
Now let’s execute the workflow and check the results. You will find something unexpected. The outer loop’s Done Branch might show a single item, or a number of items that don’t make sense, and the values are completely wrong.
This happens because n8n’s Loop node doesn’t operate like a traditional programming loop in this nested context. The inner loop executes only once on the initial data, rather than being “reset” for each item of the outer loop. This is a common point of confusion for new n8n users.
The Solution: Harnessing the Power of Sub-workflows
Fortunately, there is a simple and effective workaround that allows you to achieve the desired nested loop functionality: using an Execute Sub-workflow node.
A sub-workflow is a separate, self-contained workflow that can be triggered by another workflow. This is the key to our solution, because it allows you to create a new “execution context” for each item in the outer loop.
Step 1: Create the Inner Loop as a Sub-workflow
First, let’s build the inner loop logic in its own separate workflow. This new workflow will handle the iteration over the numbers. It will need a starting point to receive the data from the main workflow, which we’ll handle with an Execute Sub-workflow Trigger node.
- Create a new workflow.
- Add an Execute Sub-workflow Trigger node. Give it a descriptive parameter name, like
color
, which will hold the color from the outer loop. - Add the Code node with your list of numbers, just as you did before.
- Add a Loop node to iterate over the numbers.
- Inside the Loop, add a Set node. Here, you’ll set the final output, combining the
color
(from the input trigger) and thenumber
(from the current loop item).
The final sub-workflow should look like this:
Step 2: Call the Sub-workflow from Your Main Workflow
Now, let’s go back to your main workflow.
- Remove the nested Loop node you had before.
- In its place, add an Execute Sub-workflow node.
- Configure the Execute Sub-workflow node to call the sub-workflow you just created.
- Most importantly, you need to pass the current item from the main workflow’s loop into the sub-workflow. To do this, link the output of your first Loop node to the Execute Sub-workflow node. You’ll pass the
color
value into thecolor
input parameter you defined in the sub-workflow.
Your main workflow will now be much cleaner:
Step 3: Run and Verify
Now, when you execute the main workflow, you will see the results you were hoping for! For each color in your first list, the sub-workflow will be called, and it will iterate through all the numbers, creating 9 unique items (3 colors x 3 numbers).
The Takeaway
While n8n doesn’t support nested loops in the traditional sense, using a sub-workflow is a powerful and reliable pattern. It allows you to create a modular, clean, and repeatable process for handling nested data structures. As you build more complex workflows, remember this trick—it’s an essential tool for an n8n learner!