Nodes/ComfyUI-List-Utils/PyList Item Cast
ComfyUI Node

PyList Item Cast

Apply int()/float()/str() to every element at once

By godmt·Created 2 years ago·Updated 2 months ago· 16
PyList Item Cast
  • PYLIST
  • PYLIST
TYPE

So you've got a PyList and its items are all the wrong type. Maybe they came out of Split String as text and your next node wants numbers. Maybe you need to flip "0"/"1" strings into real booleans for a toggle. PyList Item Cast is the per-item version of Python's int(), float(), str() and friends - one widget, and every element gets converted.

It's a small node and it does exactly one thing, which is honestly why you'll reach for it: in a language as casually typed as Python, "list of strings" and "list of ints" look identical until a downstream node chokes on the difference.

How it works

Under the hood it's a one-liner: [try_cast(x, TYPE) for x in PYLIST]. Every item gets run through the same conversion and the result comes back as a new PyList. Nothing in-place, nothing fancy.

The two inputs that matter

  • PYLIST - the list to convert. forceInput, so it must come from an upstream PyList-producing node; there's no widget.
  • TYPE - the target type, chosen from STRING, INT, FLOAT, NUMBER, BOOLEAN, PYLIST.

The output is a single PYLIST socket holding the converted list.

Three things worth knowing about the TYPE choices, because two of them are traps:

  • NUMBER is a lie. Per the author's own tooltip, NUMBER is the same as FLOAT - it calls float() and that's it. No "pick whatever fits" smartness.
  • PYLIST as a target is a no-op. try_cast has no case for it, so every item passes through unchanged. Selecting it just relabels the socket; it's there for re-typing, not conversion.
  • BOOLEAN has opinions. Numbers become True if greater than zero, else False. Strings get parsed as floats first, then compared - so "0.5" becomes True and "" becomes False. Good enough for toggles, just don't assume it matches Python's bool() on every input.

Where the edge cases bite

The big one: INT runs int() on each element, and int("3.7") raises a ValueError, which will kill your queue mid-run. If your strings might carry decimals, cast to FLOAT first - then to INT if you really need integers. Strings that aren't numeric at all will crash either way, and there's no try/except here to save you. That's not a bug in the node; that's Python being Python, but it's the failure mode people actually hit.

Also remember what this node doesn't do: the result is still a PyList, i.e. one value on one socket. It does not start per-item processing. If you need the converted items to feed downstream nodes one at a time, chain PyList To List after this and you're done.

Installing it

Part of godmt/ComfyUI-List-Utils. In ComfyUI Manager, search "ComfyUI-List-Utils" and install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/godmt/ComfyUI-List-Utils

Restart ComfyUI and you'll find it under list_utils. No models, no extra pip dependencies - it's pure Python with no imports beyond the standard library, so there's nothing to download and nothing to break.

In practice

The workflow that'll actually use this: text in, Split String, cast the PYLIST output to INT or FLOAT, then run it through a numeric node that would have refused the string version. It's a utility node, not a showstopper - but when your seeds arrive as "42" and something downstream demands 42, it's the difference between a clean run and a head-scratching afternoon.

Categorylist_utils

Inputs (2)

NameTypeDefaultDescription
PYLISTPYLISTPyList whose items should be converted.
TYPECOMBOConversion applied to each PyList item. NUMBER is the same as FLOAT.

Outputs (1)

NameTypeDescription
PYLISTPYLISTThe PyList after converting each item.