ComfyUI Node

difference

Everything in set A that isn't in set B — a one-wire subtraction

By StableLlama·Created about a year ago·Updated a day ago· 48
difference
  • set1
  • set2
  • SET

difference is the set version of subtraction: everything in set1 that isn't in set2. It's the node you reach for when you want to exclude things - "all my tags, minus the ones on the banned list" or "all the frames I generated, minus the ones I already accepted."

It's the natural companion to union and intersection in the SET category of this pack. If union is "combine everything" and intersection is "keep only what's in both," difference is "keep only what the first one has that the second doesn't." Together they cover the three classic set relationships you'll actually use in workflow logic. Because the output is itself a SET, you can chain them - difference the result against a third set to subtract more - or feed it straight into contains, length, or convert to LIST.

How it works

Under the hood it's Python's set.difference_update: the node copies set1, removes every element found in set2, and returns the new set. Two things worth knowing about the mechanism:

  • It's non-destructive. Your set1 input is copied, not mutated. Whatever produced it still holds the original set, so you can keep using it elsewhere in the graph without surprise.
  • It's directional. A - B is not B - A. If you swap the inputs you get a different answer - the whole reason this pack also has symmetric difference for the "either but not both" case.

Inputs and output

  • set1 (SET) - the set you subtract from.
  • set2 (SET) - the set you subtract out.
  • SET (output) - elements of set1 not present in set2.

Just the two inputs, both required. If you need to subtract three or more sets, chain the node or use multiple passes.

Installing

This is part of Basic data handling by StableLlama. Install through ComfyUI Manager by searching "Basic data handling", or clone it:

cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling

Then restart ComfyUI. Like everything else in the pack it has zero dependencies and no model downloads - it's pure Python, so it installs in seconds and can't break anything else in your environment.

Common issues

The one real trap is forgetting that sets only hold hashable values. If you try to difference sets that contain lists or tensors, Python will throw an unhashable-type error the moment the node runs - that's not a bug in the node, it's the nature of sets. And keep the direction straight: if your result looks inverted, you swapped the inputs. When in doubt, wire both sets into length first and check your expectations.

CategoryBasic/SET

Inputs (2)

NameTypeDefaultDescription
set1SET
set2SET

Outputs (1)

NameTypeDescription
SETSET