cancel
Showing results for 
Search instead for 
Did you mean: 

Declare a dictionary as mixted list across multiple lines

mail_statquant
New Contributor
Hello, at my previous job I remember I got a nice trick to declare a dictionary over several lines, this was nice as it was much more readable.
Something like

d: listToDict[ (`key1; value1) \
(`key2; object2) \
(`key3; value3) \
(`key4; object4) ];

So there were a trick on how to make a list of list as a dictionary and also how to use multiline commands.
I know I could do 

d:(`key1`key2)!(`;0N);
d[`key1]:value1
...

But it is not what I want.
Would someone know this trick ?
4 REPLIES 4

abcfoots
New Contributor
q)(!). flip ((`a;1);(`b;2))
a| 1
b| 2

pressjonny0
New Contributor
Hi

The trick is usually to declare it as a list of lists, where each inner list is (key;value).  Then flip it and use ! . to turn the resulting 2 lists into a dictionary.  Something like 

(!) . flip
((`a;1);
 (`b;2);
 (`c;3))

Thanks 

Jonny


(!/) is nicer as (!/)() is ()
which is useful as a default

another option is just to use one level of nesting
and create the second programatically
for convenience (it will be somewhat slower)

q)(!/)flip 2 cut (`a;1; `b;2; `c;3)

Cheers,
   Attila


>> (!/) is nicer as (!/)() is () which is useful as a default
depends on your interpretation of nice. i'd prefer to see an exception raised during the creation of a dictionary rather than later when trying to do a lookup. but each to their own.