Nodes/ComfyUI Inspire Pack/▶Foreach List (Inspire)
ComfyUI Node Runs on cloud

▶Foreach List (Inspire)

Loop over a list and carry a running result between items

By ltdrdata·Created 3 years ago·Updated 9 months ago· 805
▶Foreach List (Inspire)
  • item_list
  • initial_input
  • flow_control
  • remained_list
  • item
  • intermediate_output

This is the start of an explicit loop in ComfyUI. It pulls items one at a time out of an ITEM_LIST and - this is the important part - lets you carry a running result from one iteration into the next. Paired with its Foreach List◀ end node, it gives you a real fold/accumulate loop, something ComfyUI's normal list handling can't do cleanly.

Here's the gap it fills. ComfyUI already runs a graph once per item when you hand it a list, but each of those runs is independent - there's no easy way to build something up across them, like stitching every image into a growing collage or keeping a running total. Foreach List adds an intermediate_output that threads through every iteration, so item 2 can see what item 1 produced. It's built on BadCafeCode's reference loop implementation for ComfyUI, which is where the graph gets the ability to loop at all.

How it works

You feed it an ITEM_LIST (build one with Worklist To Item List (Inspire)). Each pass, it emits the current item plus the current intermediate_output - you do work with them, then hand the new result to the Foreach List◀ node, which loops back here for the next item. It also emits flow_control and remained_list, the loop's internal plumbing, which connect straight to the end node.

The inputs and outputs that matter

  • item_list (ITEM_LIST) - the list to iterate over.
  • initial_input (optional) - the starting value for the accumulator. If you leave it out, the first item becomes the initial value and iteration starts from the second.

Outputs: item (the current element), intermediate_output (the running result so far), and flow_control + remained_list (wire these directly into Foreach List◀).

Installing it

ComfyUI Manager: search "Inspire Pack", install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/ltdrdata/ComfyUI-Inspire-Pack

Then restart. It's part of the Inspire Pack by Dr.Lt.Data.

Where people get tripped up

The loop only works as a matched pair - flow_control and remained_list have to go straight into a Foreach List◀ node, and your processed intermediate_output has to come back to that end node too. Break that wiring and the loop won't run (or won't terminate correctly). The other subtlety is the initial_input behaviour: leave it empty and the first list item becomes your seed value and iteration begins at the second item - great for a reduce where the first element is a natural starting point, surprising if you expected all items processed against an empty accumulator. Set initial_input explicitly when you want every item folded in. Also note the input is an ITEM_LIST, a specific Inspire type - you build it with Worklist To Item List, not from a raw ComfyUI list.

CategoryInspirePack/List

Inputs (2)

NameTypeDefaultDescription
item_listITEM_LISTITEM_LIST containing items to be processed iteratively.
initial_inputopt*If initial_input is omitted, the first item in item_list is used as the initial value, and the processing starts from the second item in item_list.

Outputs (4)

NameTypeDescription
flow_controlFOREACH_LIST_CONTROLPass ForeachListEnd as is to indicate the end of the iteration.
remained_listITEM_LISTOutput the ITEM_LIST containing the remaining items during the iteration, passing ForeachListEnd as is to indicate the end of the iteration.
item*Output the current item during the iteration.
intermediate_output*Output the intermediate results during the iteration.