ComfyUI Node

MapIndexed

Map, but your function learns its place

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

MapIndexed is Map with a second argument: the position of the current item. Where Map gives your function one value per call, MapIndexed hands it (value, index) so it can behave differently depending on where in the list it is. That's the whole difference - and it unlocks a handful of genuinely useful patterns.

The inputs

Same shape as Map:

  • function - a CLOSURE that takes two arguments: the item and its index
  • items - a LIST (from the Basic Data Handling pack, not the native Data List)
  • output - a LIST of results

Why would you want the index? A few honest examples: add the index to a seed so every batch item differs predictably; skip the first item; do a "progress marker" pass; or interleave an alternating setting based on even/odd position. None of that is easy with plain Map.

One discrepancy you should know about

The pack's own node reference claims the index starts at 1. The source code disagrees: it's a plain Python enumerate(items), which starts at 0. So the first item gets index 0, not 1. Which one is "right" depends on the version you're running - the docs are the author's intent, the code is what shipped. My advice: wire a test function that echoes the index into a text box and see which one your install gives you before you build anything dependent on it. This is exactly the kind of sharp edge the README warns about.

How it works

Identical machinery to Map: a coroutine walks the list, and each iteration expands the function's body into real graph nodes via the __IntermidiateCoroutine__ machinery, feeding back the previous result. The COMFYUI_FUNCTIONAL_COROUTINE_LIMIT (default 100) caps how many iterations run before you get a RecursionError - raise it via env var if you're iterating over genuinely long lists.

Because each iteration re-expands the function body with fresh node IDs, ComfyUI's cache can't help and every call re-runs. For cheap transforms that's fine; for anything expensive, remember you're multiplying cost by list length.

Where people get burned

  • Native Data Lists deadlock. Use LIST from Basic Data Handling.
  • Argument count mismatch. Your function must accept two arguments here, not one - the index arrives as the second parameter. If you wired a one-arg function from a Map, it'll break.
  • Index base. See above - test it. If a workflow you downloaded behaves off-by-one, this is likely why.

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. You'll want Basic Data Handling for the LIST type. If you don't need the position, plain Map is simpler - reach for MapIndexed only when the index is doing work.

Categoryduanyll/functional/high_order

Inputs (2)

NameTypeDefaultDescription
functionCLOSURE
itemsLIST

Outputs (1)

NameTypeDescription
LISTLIST