Lesson 12 / 25
Iteration Over Lists
Run the same sub-steps for every item.
Map, then collect
The iteration node takes an array (rows, reviews, document sections) and runs a sub-graph for each item, collecting the results into an output array. Use it for batch tasks: summarise each review, classify each line item, translate each paragraph. Mind cost and time: each item may call an LLM, so limit list length, run items in parallel where supported, and handle per-item failures without losing the whole run. Dify's node names, menus and options change between versions; check the current Dify documentation.
Processing a list of reviews, run
I ran this with plain Python 3 (scikit-learn 1.9.1 where imported). It models or tests one piece of a Dify app locally; Dify itself was not running. A model of an iteration node: five reviews are processed one by one (a keyword rule stands in for the LLM step), then the results are summarised: two negative reviews and two needing follow-up.
# Simplified model of an iteration node: run the same sub-steps for each item, then collect results
reviews = ["Great product, fast delivery", "Arrived broken, want refund", "Okay but pricey",
"Terrible support, refund please", "Love it"]
def sub_workflow(text): # in Dify this could be an LLM node inside the iteration
t = text.lower()
sentiment = "negative" if any(w in t for w in ["broken", "terrible", "refund"]) else "positive"
return {"text": text, "sentiment": sentiment, "needs_followup": "refund" in t}
results = [sub_workflow(r) for r in reviews] # iteration output is a list
summary = {"total": len(results),
"negative": sum(r["sentiment"] == "negative" for r in results),
"followups": [r["text"] for r in results if r["needs_followup"]]}
print(summary)
Output:
{'total': 5, 'negative': 2, 'followups': ['Arrived broken, want refund', 'Terrible support, refund please']}Cap list length
Set a maximum number of items per run and split large batches across several runs.
Quick check: What does an iteration node output?
- A single random item
- An array with one result per input item
- Nothing
- Only the first item's result
Answer
An array with one result per input item — Map over items, then collect.