Nodes/ComfyUI-GadgetNodes/Select Nth Items Of AnyList
ComfyUI Node

Select Nth Items Of AnyList

Grab any item out of any list by index — negative numbers count from the end

By 2daadv·Created 6 months ago·Updated 21 days ago· 1
Select Nth Items Of AnyList
  • any_list
  • any_list
index_list

ComfyUI hands you lists constantly - batches of images, batches of latents, lists of strings from wildcard expansion. And then you'll want "just the third one" or "only the last one" and discover there's no obvious built-in for it. Select Nth Items Of AnyList is that node: it takes any list, takes one or more indices, and returns the items at those positions. Negative indices count from the end, and out-of-range indices are silently skipped instead of crashing your run.

The author's own description says it best: "Selects the Nth items from a list. If the index is out of range, it was omit." (sic - English isn't the author's first language, and the pack's README is Japanese-first, but the behavior is unambiguous.)

How it works

The mechanism is a thin slice of Python. The input list gets flattened (nested lists collapse to one level), then for each index in your index input it does a bounds check - only indices in -len .. len-1 are kept - and pulls flattened[i]. Negative indices naturally reach from the end because that's how Python indexing works. So:

  • -1 → last item, -2 → second-to-last, and so on.
  • 0, 2, 4 → the first, third, and fifth items, in that order.
  • 999 on a 5-item list → nothing, no error.

The output is always a list, even if you select a single index. That matters for wiring: it keeps the same shape as the input, so a downstream batch node doesn't get confused.

The inputs that matter

  • any_list - any type (*), forced as an input, so wire the list in. Works with images, latents, strings, whatever.
  • index_list - an INT, also forced as an input. The tooltip spells out the negative-index trick: "Use negative values to select from the end (e.g., -1 for last item, -2 for second to last)."

The "forced input" bit is the beginner trap. You can't type an index into a box on the node - you wire an integer into it. That's easy to forget when you're used to widgets. ComfyUI's own Primitive INT nodes work, and conveniently this same pack ships a set of constant INT nodes (0, 1, -1, 64...) and an increment/decrement pair that are handy for scripting index selection. Because the node is list-aware on the input side, you can also wire a list of indices to pull several items at once.

Install

ComfyUI Manager (search "ComfyUI-GadgetNodes"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/2daadv/ComfyUI-GadgetNodes.git
pip install -r ComfyUI-GadgetNodes/requirements.txt

Restart, and it's under Gadget/logic.

Where people get burned

  • The forced-input thing again. If the node looks "stuck" and you can't type an index, that's by design - you need an INT node wired in. This catches nearly everyone once.
  • Multiple indices = multiple outputs, not one. Select 1, 3 and you get a two-item list out. If a downstream node expects a single image, you'll need to handle the list.
  • Selection is by position, not by content. It's not "the image whose filename matches," it's "the Nth thing in this exact order." Reorder upstream and you're selecting something different.

This is the kind of node that looks pointless until the moment you need it - then it's the exact missing piece between "I have 30 images" and "I want only the last one." Small, dumb, and genuinely useful.

CategoryGadget/logic

Inputs (2)

NameTypeDefaultDescription
any_list*
index_listINTThe index of the items you want to select from the list. Use negative values to select from the end (e.g., -1 for last item, -2 for second to last).

Outputs (1)

NameTypeDescription
any_list*