Back to Dictionaries

Dictionaries

Lookup By Value

Skip to 5 practice questions

Sometimes you need to search in the opposite direction — finding which key(s) are associated with a particular value.

Operations Apply Key-Wise

Arithmetic and comparison operators applied to a dictionary act on its values, returning a new dictionary with the same keys:

dict:`a`b`c`d`e!2 3 4 5 3
dict=3

The result is a boolean dictionary, `a`b`c`d`e!01001b.

where on a Boolean Dictionary

Applying where to a boolean dictionary returns the keys whose value is 1b — not positions:

where dict=3          / `b`e
first where dict=3    / `b   — first matching key
last where dict=3     / `e   — last matching key

Find (?) for Exact Matches

dict?value returns the first key whose value equals value — a concise alternative to first where dict=value for an equality test (it doesn't generalise to inequalities like >):

dict?3   / `b

Searching Nested Values

When a dictionary's values are themselves lists, compare element-wise and reduce with any each (or use in/:) before applying where:

portfolios:`Growth`Income`Balanced`Index!(101;102 103;104;105 102)
where any each portfolios=102   / `Income`Index
where 102 in/:portfolios        / `Income`Index

Both return the keys whose value contains 102 anywhere in its list.