ComfyUI Node

from keys

Every key, one shared value — and the mutable-default trap to avoid

By StableLlama·Created about a year ago·Updated 4 days ago· 48
from keys
  • keys
  • value
  • DICT

"from keys" creates a dictionary from a list of keys, all pointing at the same value. It's the pack's wrapper for Python's dict.fromkeys() - the tool for "I have these names and I want them all initialized to the same thing." You'd use it to scaffold a config dict with defaults, pre-fill lookup keys before you fill them in, or build an initialized counter structure where every key starts at the same base value.

It looks boring. It has a genuinely nasty edge case hiding in plain sight, which we'll get to.

How it works

One required input, one optional:

  • keys (LIST) - the key names. The thing you'll actually set.
  • value (*, optional) - the shared value for every key. If you leave it disconnected, every key maps to None.

The output is a DICT where each key maps to that single value. In {"a": 0, "b": 0, "c": 0} terms: same value, every key, all from one list.

The trap: mutable values are shared, not copied

This is the one that bites people, and it's worth spelling out because it's not a bug in this node - it's a property of Python's fromkeys that the node faithfully inherits. If your value is a mutable object - a LIST, a DICT, a SET - every key points to the same object instance. Change what's inside the value for one key and you change it for all of them.

So keys = ["a", "b"], value = [] gives you a dict where "a" and "b" reference one shared list. Append to dict["a"] and dict["b"] grows too. For immutable defaults (numbers, strings, booleans, None) this is a non-issue - those are fine and this node is great. If you need per-key independent mutable values, this isn't the tool; build the dict with create DICT and give each key its own copy.

When you'd reach for it

  • Seeding a config dict with defaults before selective overrides.
  • Pre-populating lookup keys at a fixed base value.
  • Counting: start every key at 0, then increment per key later.

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 node, and now you know why you shouldn't hand it a list or dict as the default.

CategoryBasic/DICT

Inputs (2)

NameTypeDefaultDescription
keysLIST
valueopt*

Outputs (1)

NameTypeDescription
DICTDICT