Nodes/ComfyUI-YogurtNodes/DictInvert (Yogurt Nodes)
ComfyUI Node

DictInvert (Yogurt Nodes)

Flip a dict upside down — values become keys, with a duplicate-safety switch

By yogurt7771·Created 2 years ago·Updated 9 days ago· 1
DictInvert (Yogurt Nodes)
  • dict_data
  • inverted_dict
handle_duplicatesfalse

DictInvert does exactly what the name says: it takes a dictionary and swaps keys and values. {"a": 1, "b": 2} becomes {1: "a", 2: "b"}. It sounds like a party trick, but inverting a mapping is a real workflow move - it's how you go from "id → name" to "name → id" lookups, or turn a frequency-count dict into "value → how many times it appeared."

The thing that decides whether this node is safe or a landmine is the handle_duplicates switch. Here's the problem: dict values are allowed to repeat, but dict keys are not. If your dict is {"a": 1, "b": 1}, a naive inversion produces {1: "b"} - one of your keys is silently destroyed. That silent data loss is the default behavior, matching Python's own dict-comprehension semantics. If that's not what you want, flip handle_duplicates on and the node keeps all keys: duplicate values become lists, so {"a": 1, "b": 1} inverts to {1: ["a", "b"]}.

How it works

With handle_duplicates off, it's a straightforward {v: k for k, v in dict_data.items()} comprehension - last key wins when values collide. With it on, it builds the result by hand: the first time a value is seen it becomes the key mapping to a single key; the second time, that single key is wrapped into a list and the new key appended. Both are plain Python, no surprises.

Inputs

  • dict_data - the dictionary to invert.
  • handle_duplicates - when true, duplicate values collect their keys into lists instead of overwriting.

Output

  • inverted_dict - the swapped dictionary, *-typed.

Install

Ships in ComfyUI-YogurtNodes:

cd ComfyUI/custom_nodes
git clone https://github.com/yogurt7771/ComfyUI-YogurtNodes.git
cd ComfyUI-YogurtNodes
pip install -r requirements.txt

Or via ComfyUI Manager (search "YogurtNodes") and restart. Under "Yogurt Nodes" → Logic.

Common issues

  • "I lost keys!" - duplicate values, handle_duplicates off. The last key won. Flip the switch to keep everything.
  • "My inverted values are lists, why?" - handle_duplicates is on, so collisions became lists. If you expected flat values, turn it off (and make sure values are unique).
  • "Can I invert a dict with unhashable values?" - no. Lists as values can't become keys (Python dict keys must be hashable). This is a limitation, not a bug.

It's a one-operation node, but the duplicate-handling decision is the whole game. Turn it on by default in data you don't fully control, and you'll never silently lose a mapping again.

CategoryYogurtNodes/Logic

Inputs (2)

NameTypeDefaultDescription
dict_data*Dictionary to invert
handle_duplicatesoptBOOLEANfalseIf True, duplicate values become lists of keys

Outputs (1)

NameTypeDescription
inverted_dict*