Nodes/comfy-ovum/_.invert
ComfyUI Node

_.invert

Flip a dict's keys and values for reverse lookups

By sfinktah·Created about a year ago·Updated 10 months ago· 7
_.invert
  • py_dict
  • DICT

_.invert swaps the keys and values of a dictionary. Have a dict that maps names to IDs and you actually need IDs to names? This is the one-command flip that turns your forward lookup into a reverse lookup. It's a tiny operation with a surprising amount of practical payoff in data plumbing.

How it works

The implementation is almost as small as the concept:

def invert(self):
    keys = self._clean.keys()
    inverted = {}
    for key in keys:
        inverted[self.obj[key]] = key
    return self._wrap(inverted)

For every key: value pair, it writes value: key into a new dict. That's it - non-mutating, returns a fresh dictionary, original untouched.

The catch is baked into the docstring: "for this to work, all of your object's values should be unique and string serializable." Your values become the new keys, and keys in Python have to be hashable. Two identical values mean one overwrites the other silently, and unhashable values (lists, dicts) would throw. Keep your values simple - strings, ints - and you're fine.

The inputs that matter

Just one real control:

  • py_dict - the primary input (*). Note the name: the generator categorized invert as an object method, so the socket is called py_dict. Also accepts a _.CHAIN to continue chaining.

Output is a single DICT - the inverted mapping.

Where you'd actually use it

Reverse lookups are the play. Classic ComfyUI setup: you've built a dict that maps model display names to file paths, or tags to seeds, and somewhere downstream you have the value but need its key - "which workflow used this exact seed?" Invert it once, store the result, and you have the O(1) lookup in the other direction.

It also composes beautifully with _.indexBy: _.indexBy builds a forward dict from a list (key by property), _.invert gives you the reverse view. Together they're a mini lookup table you can build entirely in the graph.

Installing

Ships in comfy-ovum (MIT). ComfyUI Manager → search comfy-ovum → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/sfinktah/comfy-ovum

Underscore nodes need nothing extra - the bundled underscore3 port handles them. Pack-wide requirements.txt includes pymediainfo (wants system libmediainfo on Linux), tomli-w, requests, braceexpand, pillow, numpy, markdown-it-py, aiohttp.

Gotchas

The two rules: values must be unique, and values must be hashable. Break either and you get silent data loss or a crash, not an error message you'll love. And the usual pack-level asterisk: the README calls the underscore family WIP and not production-ready. For a quick key-value flip on small metadata dicts, though, _.invert is exactly the boring utility you want - when the data cooperates.

Categoryovum/underscore

Inputs (1)

NameTypeDefaultDescription
py_dictopt*Primary input object (expected object). You can still pass any JSON-serializable value. Also accepts _.CHAIN to continue chaining.

Outputs (1)

NameTypeDescription
DICTDICT