Dictionaries
Updating & Merging Dictionaries
Once created, dictionaries are commonly filtered down to a subset of keys, merged with another dictionary, or have specific values replaced.
Selecting and Removing Keys
| Operator | Effect |
|---|---|
keys # dict (Take) | Keep only the given keys |
keys _ dict (Drop) | Remove the given keys |
positions:`AAPL`MSFT`GOOG!100 250 75
(enlist `AAPL)#positions / (,`AAPL)!,100 — Take needs a *list* of keys
`AAPL`GOOG#positions / `AAPL`GOOG!100 75
`AAPL _ positions / `MSFT`GOOG!250 75 — Drop accepts a single keyMerging Two Dictionaries
| Operator | Behaviour on matching keys |
|---|---|
, (Join) | Right-hand value always overwrites the left, even if null |
^ (Coalesce) | Right-hand value used only if the left value is null |
primary:`AAPL`MSFT`GOOG!100 0N 300
backup:`MSFT`GOOG`NVDA!250 0N 400
primary,backup / `AAPL`MSFT`GOOG`NVDA!100 250 0N 400 — right side wins on every collision
primary^backup / `AAPL`MSFT`GOOG`NVDA!100 250 300 400 — right side fills in only where primary is nullCoalesce is the natural choice when merging a primary feed with a backup feed and you don't want valid data overwritten by nulls.
Replacing Values In Place
The Amend operator @[list;indices;:;value] replaces elements at given positions. Combined with where, it's the standard way to find-and-replace values inside a dictionary's value list:
x:101 102 103 102
where x=102 / 1 3 — positions where x equals 102
@[x;where x=102;:;0N] / 101 0N 103 0N — those positions replaced with nullApply this each over value dict, then re-pair with key dict using ! to rebuild the updated dictionary.
Practice Questions
Test your understanding with 3 hands-on questions