Nodes/Kinburg-Nodes/For Each (Collect)
ComfyUI Node

For Each (Collect)

The receiving end of a map loop

By Kinburg·Created 3 months ago·Updated 6 days ago· 1
For Each (Collect)
  • flow
  • result_0
  • result_1
  • result_2
  • result_3
  • result_4
  • result_5
  • result_6
  • result_7
  • result_8
  • result_9
  • result_10
  • result_11
  • result_12
  • result_13
  • result_14
  • result_15
  • result_16
  • result_17
  • result_18
  • result_19
  • collected_0
  • collected_1
  • collected_2
  • collected_3
  • collected_4
  • collected_5
  • collected_6
  • collected_7
  • collected_8
  • collected_9
  • collected_10
  • collected_11
  • collected_12
  • collected_13
  • collected_14
  • collected_15
  • collected_16
  • collected_17
  • collected_18
  • collected_19

ComfyUI's execution graph is acyclic - there's no such thing as a "loop" in the native engine. So the pack builds one out of two nodes, and For Each (Collect) is the closing half: the sibling that takes the per-iteration results from a For Each (Open) map and gathers them back into a list. Think of the pair as a map in any other language - Open fans a batch out one item at a time, the graph between the two nodes runs once per item, and Collect reassembles the outputs on the other side.

The wiring is deliberately foolproof: connect the matching For Each (Open)'s flow output into this node's flow input (there's an Auto-pair button that does exactly this), and the loop is complete. The flow bundle carries everything Collect needs - which Open it belongs to, the current index, the total count, and the accumulator state. From there it's hands-off: you wire up to 20 result_* inputs, one per slot you want collected, and read the corresponding collected_* outputs.

The mechanics are worth one sentence because they explain the one gotcha. Collect runs inside ComfyUI's graph expansion (the expand return) - each pass it clones the loop body, pushes the next index into the cloned Open, and recurses, carrying the accumulated lists along. On the final pass it stops expanding and hands back the accumulated items as plain Python lists per slot. And here's the catch: it deliberately does not fan those lists out as ComfyUI list outputs itself, because inside graph expansion ComfyUI flattens list outputs across slots, which corrupts the slot count. The pack's answer is a List Output node - the 🔗 button wires one up for you - which turns the collected lists into real per-item ComfyUI lists downstream. That's the one piece of plumbing you should let the button handle rather than doing by hand.

Installing

Ships in Kinburg-Nodes: ComfyUI Manager → search "Kinburg-Nodes", or:

cd ComfyUI/custom_nodes
git clone https://github.com/Kinburg/Kinburg-Nodes
# restart ComfyUI

No dependencies beyond ComfyUI's bundled torch. It lives in the flow/loops package alongside the For Each (Open), Repeat and While families.

Gotchas

The most common mistake is treating Collect like a normal node and expecting the outputs while the loop is mid-flight - it only emits the accumulated result when the last iteration lands. And because it iterates to the shortest connected input (the Open iterates to the shortest input), mismatched input lengths can silently drop the tail of the longer one. Keep your per-slot inputs the same length and the collect is exact. Different-sized images also come out as separate list items rather than being forced into one batch, which is usually what you want - if you actually need one tensor, that's what Image Batch Insert or the batch nodes are for.

CategoryKinburg-Nodes/flow/loops

Inputs (21)

NameTypeDefaultDescription
flowKINBURG_LOOP_FLOWConnect the matching For Each (Open)'s 'flow' output (the Auto-pair button does this).
result_0opt*
result_1opt*
result_2opt*
result_3opt*
result_4opt*
result_5opt*
result_6opt*
result_7opt*
result_8opt*
result_9opt*
result_10opt*
result_11opt*
result_12opt*
result_13opt*
result_14opt*
result_15opt*
result_16opt*
result_17opt*
result_18opt*
result_19opt*

Outputs (20)

NameTypeDescription
collected_0*
collected_1*
collected_2*
collected_3*
collected_4*
collected_5*
collected_6*
collected_7*
collected_8*
collected_9*
collected_10*
collected_11*
collected_12*
collected_13*
collected_14*
collected_15*
collected_16*
collected_17*
collected_18*
collected_19*