cancel
Showing results for 
Search instead for 
Did you mean: 

A question about dictionary in Q

jerry_xue_dev
New Contributor
Hi,

I started learning kdb+/q recently. I got a problem while create a dictionary.

According to the online guide, a dictionary could be created like this:

q)dict:`items`sales`prices!(items;sales;prices)

However, when I tried this:

q)dict:`items!(items)

I got error message: `type.

Anyone know why it is so? Thanks!
3 REPLIES 3

charlie
New Contributor II
New Contributor II
a dictionary is a map from one list to another.
hence the args must be lists/vectors, not scalars/atoms.
use enlist to form a list from a scalar.


dataGuyJosh
New Contributor II

As charlie described, a dictionary with one element needs to be enlisted, a shorthand for this might look like

d:d!d:enlist`items

 

That does construct a singleton dictionary but the keys and values are the same, which might not be what the OP wanted.

 

q)show d:d!d:enlist`items
items| items

 

Happily the 1 f\ ‘Zen monks’ pattern can be used to convert a variable name into a dictionary of its name and its value.

 

q)items:`cow`sheep`cat`dog
q).[!] enlist each 1 value\`items
items| cow sheep cat dog

 

I’ll write more about the Zen monks soon.