ComfyUI Node

array_Slice

Python-style slicing for the pack's arrays — with tensor stacking as a bonus

By cardenluo·Created 2 years ago·Updated a day ago· 324
array_Slice
  • array
  • array
start0
end-1

You know how Python's array[2:5] just works? array_Slice brings that to this pack's arrays. Give it an array and a start/end, and it returns the sub-array - with negative indexing and the default end = -1 meaning "through the last element." It's the trim tool for prompt arrays, seed lists, and collected stage values, and it has a genuinely useful extra: if the slice contains same-shape tensors, it stacks them into a single tensor, so slicing a list of images hands you a ready-made batch.

How it works

The slice logic mirrors Python faithfully: negative start/end are interpreted from the end of the array, both are clamped into range, and the result is array[start:end]. One deliberate difference from Python - if start ends up greater than end, instead of returning a weird reversed slice it returns an empty result (an empty tensor if the input held tensors, an empty list otherwise). That's a safety choice: it fails soft rather than handing you something that'll confuse the next node.

The tensor behavior deserves its own callout. When the sliced elements are tensors and they all have matching shapes, the node tries torch.stack and returns one stacked tensor. That's how you turn "images 3 through 7 of my reference list" into an actual image batch in one step. If shapes don't match, it falls back to returning the plain list - no crash, just a list.

Inputs and outputs

Inputs: array (a LIST), start (default 0), end (default -1). Output: array (wildcard type - it'll come out as whatever you put in, stacked if applicable). With defaults it's a pass-through of the whole array, which makes it safe to wire in early and adjust later.

Where you'd use it

Trim a collected prompt array before it hits a batch node, drop the first N of a seed list to re-roll just the tail, or extract a frame range from an image array that some other node already assembled. It's the natural partner to create_array and flow_stage_list on the gathering side, and array_Merge on the joining side.

Install

Part of ComfyUI-Apt_Preset: ComfyUI Manager → search "ComfyUI-Apt_Preset", or clone into custom_nodes + install.bat, restart. No models. Only real gotcha: the start/end are interpreted against the array you give it, and with the clamped-to-range behavior, out-of-range indices quietly clamp rather than error - so a wrong index won't tell you it's wrong.

CategoryApt_Preset/data/list|Array

Inputs (3)

NameTypeDefaultDescription
arrayLIST
startINT0
endINT-1

Outputs (1)

NameTypeDescription
array*