invert
Flip a dict — keys become values, values become keys
- input_dict
- inverted_dict
- success
"invert" swaps a dictionary's keys and values. {"red": 1, "blue": 2} becomes {1: "red", 2: "blue"}. You'd reach for it when you have a lookup that's pointing the wrong way - a name-to-id map you suddenly need as id-to-name, a value-to-label dict you need as label-to-value - and you don't want to rebuild it by hand.
This is the rare node in the DICT section with two outputs, and the second one exists for a very good reason: inversion can fail, and the failure mode is subtle enough that you want a signal.
How it works
input_dict(DICT) - the dictionary to flip.- Output
inverted_dict(DICT) - the swapped result. - Output
success(BOOLEAN) - whether the inversion succeeded.
Two constraints decide success:
Values must be hashable. Dict keys have to be hashable, so when your values become keys, they have to qualify too. Numbers, strings, booleans, tuples - fine. Lists and dicts as values - not hashable, and inversion fails. In that case the node returns the original dict unchanged with success = False. That's why the boolean output exists: it's your cue that the flip didn't happen and downstream shouldn't trust the result.
Duplicate values collapse. If two keys map to the same value, the inverted dict can only hold one of them, and the last one wins. {"a": 1, "b": 1} inverts to {1: "b"} - "a" is silently gone. No error, no warning. If your values aren't unique, inversion loses data by construction.
The pattern that keeps you safe
Wire success somewhere visible (a display node, or a control-flow check) whenever the dict might contain unhashable or duplicate values. The node is honest about failure - that's the whole reason it reports success - but only if you actually look at it. For a dict you know is well-behaved (string values, all unique), you can safely ignore the second output.
When you'd reach for it
Reverse lookups are the classic case: you've got score → label and you want label → score. It also composes with the rest of the pack - invert, then get by what used to be a value, or invert before serializing to a different orientation.
Install
Ships in Basic data handling - pure Python, zero dependencies, no model downloads. ComfyUI Manager → search "Basic data handling", or:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
then restart ComfyUI. Useful, with a built-in canary - just remember to read it.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| input_dict | DICT | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| inverted_dict | DICT | — |
| success | BOOLEAN | — |