12
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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
HyperGroups
  • 8,619
  • 1
  • 26
  • 63

1 Answers1

5

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • In version 11, the line of code with KeyDrop now returns {<|2 -> 2|>, <|1 -> 1, 2 -> 2|>, <|2 -> 2|>} –  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 05:12