# Unexpected behavior of KeyTake

list = {{a, b, c, e}, {1, 2, b, h}, {"a", "h", 3, 5}};
asso = GroupBy[list, {#[[1]], #[[2]]} &]

<|{a, b} -> {{a, b, c, e}},
{1, 2} -> {{1, 2, b, h}},
{"a","h"} -> {{"a", "h", 3, 5}}|>


There is a problem in this code, since "a" and "h" might be two keys. However, KeyExistsQ is not Listable, but KeyTake is.

KeyExistsQ[asso, {"a", "h"}]

True

KeyTake[asso, {"a", "h"}]

<||>


So I wrap Key around {"a", "h"} to eliminate ambiguity.

KeyTake[asso, Key[{"a", "h"}]]

<||>


Contrast the above with

KeyDrop[asso, Key[{"a", "h"}]]

<|{a, b} -> {{a, b, c, e}}, {1, 2} -> {{1, 2, b, h}}|>


Have I found a bug? Tested on V10.4

• KeyTake[asso, {{"a", "h"}}]... – ciao Apr 11 '16 at 8:28
• I would have expected Key to work. I think you should report it to WRI. – Edmund Apr 11 '16 at 9:50
• @ciao glad to see that, where do you see such examples in Help Page？ or You just tried it out? – HyperGroups Apr 12 '16 at 12:51
• @HyperGroups - it is how it's documented, not sure what you're asking: {x,y,...} is an argument with multiple keys, {{x,y,...}} is an argument with a single, compound key. – ciao Apr 12 '16 at 22:28
• I think there is a real issue here and the question should not be closed. – m_goldberg Jul 2 '16 at 23:58

The behavioral discrepancy can be shown with a very simple example.

assoc = <|1 -> 1, 2 -> 2|>;
{KeyTake[assoc, 1], KeyTake[assoc, Key[1]], KeyTake[assoc, Key[1][assoc]]}


{<|1 -> 1|>, <||>, <|1 -> 1|>}

{KeyDrop[assoc, 1], KeyDrop[assoc, Key[1]], KeyDrop[assoc, Key[1][assoc]]}


{<|2 -> 2|>, <|2 -> 2|>, <|2 -> 2|>}

Surely the behavior of KeyTake[assoc, Key[1]] and KeyDrop[assoc, Key[1]] should be more consistent. Personally, I feel it is the behavior of KeyDrop that is wrong.

### Update

I reported this issue to Wolfram tech support. Yesterday, 2016-Jul-7, I received a reply from which I quote the relevant section.

It does appear that KeyDrop is not behaving consistently. I have forwarded an incident report to our developers with the information you provided. If I receive any progress on this issue, you will be posted regarding the same.

This confirms my suspicion that it is KeyDrop and not KeyTake that is behaving badly.

• In version 11, the line of code with KeyDrop now returns {<|2 -> 2|>, <|1 -> 1, 2 -> 2|>, <|2 -> 2|>} – user31159 Aug 23 '16 at 23:00
• @Xavier. I don't have V11, but if that's the case, KeyDrop has been fixed in V11. I suggest you update the question to mention the fact. – m_goldberg Aug 24 '16 at 5:12