Dictionaries · Lookup By Value · Practice Question
Filter Dictionary Keys by Value
Find Dictionary Keys Containing a Value
Question
A trading system maintains a dictionary mapping portfolio names to the IDs of instruments held within each portfolio:
portfolios:`Growth`Income`Balanced`Index!(
101;
(102;103);
104;
(105;102)
)Extract all portfolio names whose holdings contain instrument ID 102.
Expected result:
`Income`IndexSolution
where any each portfolios=102Result:
`Income`IndexAlternative Solution
where 102 in/: portfoliosResult:
`Income`IndexExplanation
portfolios=102 compares 102 against every value — scalars produce a single boolean, lists produce a boolean list. any each collapses each result to a single boolean (true if 102 appears anywhere in that value), and where returns the matching keys.
102 in/:portfolios expresses the same membership test more directly and is generally the more readable choice. Equivalently: (key portfolios) where 102 in/:value portfolios.