kdbunion-join

Union of list of dictionaries in KDB


I am trying to join an arbitrarily long list of dictionaries into one dictionary where matching keys will take the union of distinct values and new keys are simply inserted. Is there a way to do that using uj?

dd1:(`a`b`c)!(1 2; 3 4; 5 6);
dd2:(`b`c`d)!(7 8; 9 10; 0 1);
dd3:(`a`f`e)!(3 4; 1 1; 2 2);

(uj)/:(dd1;dd2;dd3) just returns the last dict.

What I want is:

(`a`b`c`d`e`f)!(1 2 3 4; 3 4 7 8; 5 6 9 10; 0 1; 2 2; 1 1)

Solution

  • You could use union as opposed to your original union join attempt:

    q)(union'/)(dd1;dd2;dd3)
    a| 1 2 3 4
    b| 3 4 7 8
    c| 5 6 9 10
    d| 0 1
    f| ,1
    e| ,2
    

    This handles the unique values too