« How can I test for the existence of a file or directory in q? | Main | I have a very big text file. Can I read a text file in chunks? »

How do I remove a key from a dictionary?

Pass the key you want to remove as the left argument to the _ (drop) operator, and pass the dictionary as its right argument:

q)dict: `a`b`c ! 1 2 3
q)dict
a| 1
b| 2
c| 3
q)`b _ dict
a| 1
c| 3
q)

If you reverse the order of the arguments, the removal still works:

q)dict _ `b
a| 1
c| 3
q)

Although we can't think of a good reason to do this, you might come across it. As our commenter Attila pointed out, however, you are more likely to see this overload in its assignment form:

q)dict _: `b / same as dict: dict _ `b
q)dict
a| 1
c| 3
q)

Meanwhile, you can remove multiple keys at once by passing a list of keys as the left argument to _ (drop):

q)`a`c _ dict
b| 2
q)

Reversing the arguments does not work in this case:

q)dict _ `a`c
'type
q)

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

so that

dict _:one_key

works, that comes up a few times.

April 20, 2011 | Unregistered CommenterAttila

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.