# Selecting Keys when Lists are used as Keys

Analyzing a sequence after restructuring it, Counts returns a sequence similar to below:

assoc = <|{0,1}->8,{1,1}->6,{1,0}->3,{1,-1}->10,{-1,-1}->9,{-1,1}->6,{-1,0}->5,{0,-1}->1|>


I'm only trying to select certain key-values, for example,

keys = {{0, 1}, {1, 0}, {1,-1}, {-1,1}}


Something like KeyTake[assoc, keys] or KeySelect[assoc, keys] is returning empty an association <||>. Forcing the list to be a key isn't working either, such as KeySelect[assoc, Key[{1, 0}]]. I'm fairly certain there is an elegant way to do this in Mathematica. Does anyone have any suggestions?

• Are you sure that KeyTake[assoc, keys] does not work? It works for me as expect. I am using Mathematica version 12.3.1.0. Maybe this is a version-dependent issue. Aug 24, 2021 at 16:17

Excuse me, this was too much for the comment section, but using your own values (and suggestions), it seems to work on 12.2.0 Win7 x64. As I was typing this Henrik Schumacher posted the comment as well. I think you should do: Evaluation: Quit Kernel and try again.

assoc = <|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} ->
10, {-1, -1} -> 9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>

<|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} -> 10, {-1, -1} ->
9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>

Keys[assoc]

{{0, 1}, {1, 1}, {1, 0}, {1, -1}, {-1, -1}, {-1, 1}, {-1, 0}, {0, -1}}

Values[assoc]

{8, 6, 3, 10, 9, 6, 5, 1}

keys = {{0, 1}, {1, 0}, {1, -1}, {-1, 1}}

KeyTake[assoc, keys]

<|{0, 1} -> 8, {1, 0} -> 3, {1, -1} -> 10, {-1, 1} -> 6|>

• Bah. Restarting MMA did it. on 12.3. Aug 24, 2021 at 16:36
assoc = <|{0, 1} -> 8, {1, 1} -> 6, {1, 0} -> 3, {1, -1} ->
10, {-1, -1} -> 9, {-1, 1} -> 6, {-1, 0} -> 5, {0, -1} -> 1|>
keys = Key /@ {{0, 1}, {1, 0}, {1, -1}, {-1, 1}}
assoc[[keys]]