Back to Lookup By Value

Dictionaries · Lookup By Value · Practice Question

Filter Dictionary Keys by Value

Sign in to track your progress

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`Index

Solution

where any each portfolios=102

Result:

`Income`Index

Alternative Solution

where 102 in/: portfolios

Result:

`Income`Index

Explanation

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.