Nodes/comfy-ovum/_.lastIndexOf
ComfyUI Node

_.lastIndexOf

Find the LAST match in a list — and fix that fromIndex default first.

By sfinktah·Created about a year ago·Updated 10 months ago· 7
_.lastIndexOf
  • py_list
  • value
  • INT
fromIndex0

_.lastIndexOf is the right-to-left version of finding something in a list: it returns the index of the last occurrence of a value, or -1 if the value isn't there. Useful whenever a list can contain duplicates and you care about the newest match - the last time a filename repeats, the last seed that came up, the final occurrence of a tag. But there's a default value in this node that will silently break your first attempts, so read this before wiring it up.

What it is

Part of the ovum/underscore family in sfinktah/comfy-ovum, wrapping the lastIndexOf method of underscore3, a Python port of Underscore.js. The port implements it with proper JS semantics: a negative fromIndex counts from the end, out-of-range values are clamped, and the search walks backward from the starting point.

Inputs and outputs

  • py_list (type *) - the list to search.
  • value (type *) - what you're looking for.
  • fromIndex (INT, default 0) - where the backward search starts.

Output is properly typed INT - one of the few in the family the generator typed correctly. -1 means not found. Feed a _.CHAIN and you get a chain back instead, resolved with _.value.

The gotcha that will bite you

The fromIndex widget defaults to 0. In this port, a provided 0 means "start the backward search at index 0" - which means it only ever checks index 0. So with the default settings, _.lastIndexOf returns 0 if the value is at the front of the list, and -1 for everything else. You will absolutely wire this up, search for something you know is in the list, get -1, and be confused for an hour.

The fix is simple: set fromIndex to the list's length (or beyond - it clamps). Then the search genuinely starts at the tail and walks left, which is the lastIndexOf you wanted. If you're indexing dynamically, feed it len(list) from a length node. This is a real trap in the current implementation, not a theoretical one.

Installing comfy-ovum

ComfyUI Manager: search for comfy-ovum, install, restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/sfinktah/comfy-ovum
pip install -r comfy-ovum/requirements.txt

Restart and you're set. No model downloads, no CUDA - pure-Python utilities. And yes, this is the kind of wart the README's "work in progress" warning is about. The node is fine once you know the default; the knowing part is the whole game.

Categoryovum/underscore

Inputs (3)

NameTypeDefaultDescription
py_listopt*Primary input object (expected array). You can still pass any JSON-serializable value. Also accepts _.CHAIN to continue chaining.
valueopt*value: JSON allowed for arrays/objects where applicable.
fromIndexoptINT0fromIndex (int)

Outputs (1)

NameTypeDescription
INTINT