Nodes/Basic data handling/filter by keys
ComfyUI Node

filter by keys

Keep only the keys you name — a whitelist for your dict

By StableLlama·Created about a year ago·Updated about 15 hours ago· 48
filter by keys
  • input_dict
  • keys
  • DICT

"filter by keys" takes a dictionary and a list of keys, and returns a new dictionary containing only the entries whose keys are in that list. It's the whitelist counterpart to exclude keys (blacklist). This is the node you want when a big dict feeds something that only cares about a handful of fields - say, a save node that writes specific metadata, or a formatter that needs three named values from a config blob with twenty.

The asymmetry between the two filters is worth internalizing: with exclude, a typo in your key list leaks an entry you wanted gone. With filter, a typo in your keep-list just means a smaller dict - the damage is visible immediately. That makes filter the safer default for anything sensitive, and it's why it tends to be the one people reach for first.

How it works

Two required inputs:

  • input_dict (DICT) - the dictionary to filter.
  • keys (LIST) - the keys to keep.

Only keys that actually exist in the original dict are retained - a key you name that isn't there is silently skipped, not added with a blank value. The implementation picks whichever iteration strategy is faster for the relative sizes, deduplicates your key list, and returns a new dict. Non-destructive: the input is untouched.

Inputs and outputs

  • input_dict (DICT)
  • keys (LIST)
  • Output: DICT - the filtered copy.

Gotchas

  • keys must be a LIST. Create it with create LIST, or pull it from another node - a bare comma-string won't connect to the port.
  • Empty key list returns an empty dict (with the input dict's type preserved). Empty input dict returns the empty input.
  • If every key you asked for happens to survive, the node returns the original object rather than a pointless copy - so don't be surprised if identity feels "shared" in that corner case.

Where it fits

The most common shape: a rich dict built up in one corner of the graph, then filtered differently for different consumers - one branch keeps metadata fields for a writer, another keeps only the numeric settings for a math node. Combine it with create DICT for assembly and get for retrieval and you've got a tidy little data pipeline. If your need is "everything except a few," switch to exclude keys; if it's "just these," filter is your friend.

Install

Part of 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.

CategoryBasic/DICT

Inputs (2)

NameTypeDefaultDescription
input_dictDICT
keysLIST

Outputs (1)

NameTypeDescription
DICTDICT