Back to Creating Dictionaries

Dictionaries · Creating Dictionaries · Practice Question

Create a dictionary - 3 ways

Sign in to track your progress

Question

Create a dictionary named p with the keys a, b, and c, and the following corresponding values:

a -> `AAPL
b -> 10.0
c -> 123i

Solution

Method 1: Using !

p:`a`b`c!(`AAPL;10.0;123i)

Result:

a| AAPL
b| 10f
c| 123i

Method 2: Using dictionary literal syntax

p:([a:`AAPL;b:10.0;c:123i])

Result:

a| AAPL
b| 10f
c| 123i

Method 3: Applying ! with dot apply

p:(!) . (`a`b`c;(`AAPL;10.0;123i))

Result:

a| AAPL
b| 10f
c| 123i

Method 4: Projection with dot apply

([a: ;b:; c:]) . (`AAPL;10.0;123i)

Result:

a| `AAPL
b| 10f
c| 123i

(Here the values form a general list, so the console shows the symbol with its backtick.)

Explanation

All four build the same dictionary. ! is the canonical operator, pairing the key vector `a`b`c with the general-list value (AAPL;10.0;123i). The literal form ([a:...])is more self-describing but restricted to symbol keys. Dot apply(!) . (keys;values)` is useful when the key and value lists are already held as a single 2-item list.