Dictionaries
Converting Dictionaries to Tables
A table in kdb+ is just a flipped column dictionary. Recognising when a dictionary (or list of dictionaries) is table-shaped is essential for working with both structures interchangeably.
Column Dictionaries and flip
A column dictionary is a dictionary whose values are all lists of the same length — each value represents a column:
d:`a`b!(1 2 3;4 5 6)
flip da b
---
1 4
2 5
3 6If the value lists have unequal lengths, flip fails with 'length.
Conforming Dictionaries
Two or more dictionaries conform when they have:
- The same keys
- In the same order
A list of conforming dictionaries is automatically promoted to a table:
A:`a`b!10 20
B:`a`b!20 30
(A;B)a b
-----
10 20
20 30Value types don't need to match between the dictionaries — only the key names and order matter. type (A;B) returns 98h, the table type code.
If the key order differs (e.g. `a`b vs `b`a), the dictionaries do not conform and (A;B) remains a plain list of two dictionaries.
A table is a flipped column dictionary — .Q.s1 reveals this representation: .Q.s1 (A;B) returns "+`a`b!(10 20;20 30)", where the leading + (flip) marks the column dictionary.
Finding Common Keys
When combining dictionaries that don't share an identical key set, use key each together with inter to find the keys common to all of them, then # (Take) to restrict each dictionary to just those keys — producing a conforming list ready to become a table:
dicts:(`a`b`c!1 2 3; `b`c`d!4 5 6; `c`b!7 8)
common:(inter/) key each dicts / `b`c
common #/: dictsb c
---
2 3
4 5
8 7Practice Questions
Test your understanding with 4 hands-on questions