ComfyUI Node

TakeWhile

Take the front of the list, stop when it stops fitting

By Duanyll·Created 11 months ago·Updated 4 months ago· 2
TakeWhile
  • items
  • predicate
  • LIST

TakeWhile is Select's impatient cousin. Select checks every item and keeps the keepers; TakeWhile walks from the front of the list and stops at the first item that fails the predicate - everything after that is dropped, no matter how good it is. It's itertools.takewhile for ComfyUI, and it's the node you want when order carries meaning.

The inputs

  • items - the LIST you're consuming (Basic Data Handling's LIST).
  • predicate - a CLOSURE taking one item and returning a BOOLEAN.
  • output - a LIST of the leading items that passed, in order.

The difference from Select is the whole point. Take [5, 4, 6, 1] with the predicate "greater than 3": Select keeps every passing item and returns [5, 4, 6]; TakeWhile also returns [5, 4, 6] here, because nothing fails until 1. Now try "is even" over [2, 4, 7, 8]: Select returns [2, 4, 8], but TakeWhile stops dead at 7 and returns [2, 4], never even looking at 8. The stop is at the first failure, not at the end.

When you actually need it

TakeWhile makes sense when the list has a natural ordering and you care about the prefix: chunking data until a size or count is reached, consuming frames until a scene change, reading settings until a sentinel value, or "take every valid frame from the start of a burst until the first bad one." For most other cases - "keep everything that passes" - Select is the correct and simpler node.

How it works

The usual coroutine loop, but with an early exit: iterate items, yield the predicate per item, append while it's True, and break on the first False. The item that failed is not included - the list ends exactly at the last passing element. Each predicate call re-expands the closure body into real graph nodes (uncached), and COMFYUI_FUNCTIONAL_COROUTINE_LIMIT (default 100) still bounds the loop.

Where people get burned

  • Confusing it with Select. If you expected filtering and got a truncated prefix, this is it. Read the semantics again: first failure ends the list.
  • The failing item disappears. TakeWhile doesn't hand you the item that broke the streak. If you need it, you'll want a predicate on the next node or a different approach.
  • Native Data List deadlock - convert to LIST via Basic Data Handling.

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; install Basic Data Handling for LIST support. Niche, but when a streaming or chunking workflow calls for it, nothing else in the pack does this.

Categoryduanyll/functional/high_order

Inputs (2)

NameTypeDefaultDescription
itemsLIST
predicateCLOSURE

Outputs (1)

NameTypeDescription
LISTLIST