12
$\begingroup$
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

$\endgroup$
6
  • 4
    $\begingroup$ KeyTake[asso, {{"a", "h"}}]... $\endgroup$
    – ciao
    Commented Apr 11, 2016 at 8:28
  • $\begingroup$ I would have expected Key to work. I think you should report it to WRI. $\endgroup$
    – Edmund
    Commented Apr 11, 2016 at 9:50
  • $\begingroup$ @ciao glad to see that, where do you see such examples in Help Page? or You just tried it out? $\endgroup$ Commented Apr 12, 2016 at 12:51
  • 2
    $\begingroup$ @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. $\endgroup$
    – ciao
    Commented Apr 12, 2016 at 22:28
  • 2
    $\begingroup$ I think there is a real issue here and the question should not be closed. $\endgroup$
    – m_goldberg
    Commented Jul 2, 2016 at 23:58

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ In version 11, the line of code with KeyDrop now returns {<|2 -> 2|>, <|1 -> 1, 2 -> 2|>, <|2 -> 2|>} $\endgroup$
    – user31159
    Commented Aug 23, 2016 at 23:00
  • $\begingroup$ @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. $\endgroup$
    – m_goldberg
    Commented Aug 24, 2016 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.