ComfyUI Node

Map

The for loop ComfyUI never gave you

By Duanyll·Created 11 months ago·Updated 4 months ago· 2
Map
  • function
  • items
  • LIST

Map is the "apply this function to every element" node - Python's map(), but inside ComfyUI. Feed it a list and a function, and it calls the function once per item, collecting the results into a new list of the same length. If you've ever wanted to run the same processing over twenty images without pasting twenty copies of the same nodes, this is the loop you were looking for.

The inputs

  • function - a CLOSURE (from a Function End). It takes one argument per call.
  • items - a LIST from the Basic Data Handling pack. This bit matters: ComfyUI's native Data List will hang the graph. Convert first.
  • output - a LIST of results, in the same order as the input.

A concrete thought: define a function that takes an image and returns an upscaled version, build a LIST of images, and Map gives you a LIST of upscales. No node soup, no copy-paste.

How it works

This is where the pack shows off. Each high-order node is backed by a Python coroutine: Map iterates over your list and, for each item, yields the function plus that item's arguments. The pack's internal machinery turns each yield into a real, executable subgraph - the __IntermidiateCoroutine__ node keeps expanding the function body, feeding the previous result back in, until the list is done. From your side it looks like one node doing a loop; underneath it's a graph being rebuilt iteration by iteration.

Two knobs exist in the environment, not the UI:

  • COMFYUI_FUNCTIONAL_COROUTINE_LIMIT (default 100) - how many iterations the coroutine machinery will do before it throws a RecursionError. A long list can hit this; raise it if you're looping over hundreds of items for real.

Because the closure body expands with fresh node IDs each iteration, ComfyUI's cache doesn't help here, so big Maps cost real runtime. If you're mapping a cheap numeric transform over a hundred items, fine. If you're mapping a full KSampler over a hundred images, budget accordingly - that's a hundred sampler runs.

Where people get burned

  • Data List vs LIST. The deadlock is real and it's the #1 complaint pattern for this pack. Always convert via Basic Data Handling.
  • Non-serializable values. Map runs locally, so it can move images and tensors fine - but if your function's inputs and outputs aren't plain values, keep everything inside the function body.
  • Expect it to be slow-ish. Each call re-runs the body from scratch. That's by design, but don't Map a giant model-loading function and expect miracles.

Installing it

Ships in Duanyll/comfyui_functional. ComfyUI Manager: search "Duanyll/comfyui_functional", or:

cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/comfyui_functional
# restart ComfyUI

No models, no pip deps - but do install Basic Data Handling (StableLlama/ComfyUI-basic_data_handling), because without it you can't build the LIST this node eats. Map, along with MapIndexed, is probably the first high-order node you'll actually use.

Categoryduanyll/functional/high_order

Inputs (2)

NameTypeDefaultDescription
functionCLOSURE
itemsLIST

Outputs (1)

NameTypeDescription
LISTLIST