index
Index — find where something lives, or get a -1 that means 'nowhere'
- list
- value
- index
index is the reverse of get item: instead of "give me what's at position 5," it answers "where does this value live?" Give it a LIST and a value, and it returns the position of that value's first occurrence as an integer. It's the node you reach for when you need to find something's place before you act on it - "which frame is the failure at?", "what position in the sweep is my target value?"
How it works
It's Python's list.index(), with the failure mode softened:
try:
return (list.index(value, start, end),)
except ValueError:
return (-1,)
The important difference from raw Python: a missing value returns -1, not an error. list.index() would raise ValueError and kill the node; this returns -1 so your graph keeps running. Treat -1 as "not found" and branch on it - a == -1 check feeding into if/else is the classic pattern.
Two optional inputs extend it: start (default 0) and end (default -1). Setting start limits the search to a slice of the list - useful when the same value appears multiple times and you want the second occurrence, which you'd get with index(value, first_position + 1). The default end = -1 means "all the way to the end," and the node internally converts it to the list length before searching.
Where you'd use it
- Locating a value before modifying it. Find where a prompt is, then
insertorget itemat that spot. - Membership with position.
containsonly tells you yes/no;indextells you where, and-1doubles as a no. - Second-occurrence hunting with a non-zero
start.
Installing
Part of the "Basic data handling" pack by StableLlama. ComfyUI Manager: search "Basic data handling", install, restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
Restart ComfyUI. Zero dependencies, no models - pure Python, a very low-friction install.
Gotchas worth knowing
-1means "not found," and-1is also a legal list position from the end - don't confuse the sentinel with an actual index when you feed it toget item.- Matching is exact equality: case, whitespace, and type all matter.
- It returns the first occurrence unless you raise
start. And it works on the pack's LIST type, not a native data list.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| list | LIST | — | |
| value | * | — | |
| startopt | INT | 0 | — |
| endopt | INT | -1 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| index | INT | — |