Search code examples
dictionarykdb

KDB: In-place delete from dictionary


To upsert an element into the dictionary, I do

q) d[`x]:12345

This modifies existing dictionary and the operation cost is close to O(1) or O(log N) depending on the underlying implementation (hash table or tree) (I don't know in fact).

However, to remove a key I must use:

q) d:(enlist `x) _ d

Which is at least O(N) because it copies the full dictionary without deleting items in O(N) then assigns is to d in O(1) because of pointer.

This looks like delete operation discrimination! Maybe in-place delete is poorly documented but exists somewhere?


Solution

  • Two more options include apply:

    .[`d;();_;`x]
    

    functional delete

    ![`d;();0b;enlist`x]
    

    The last form is useful if you want to delete multiple keys in one go. E.g.,

    ![`d;();0b;`x`y]