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$ Commented Apr 11, 2016 at 8:28
  • $\begingroup$ I would have expected Key to work. I think you should report it to WRI. $\endgroup$ 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$ 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$ 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$ 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$ Commented Aug 24, 2016 at 5:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.