# Mathematica association does not work on symbols

Question about using an association within Replace.

If I have an association which contains symbols like so:

dict = <|Subscript[r, 1] -> 1, Subscript[r, 2] -> 2|>


and I do some operation using these variables for e.g.

lst = f[#1[[1]], #[[2]]] & /@ {{Subscript[r, 1], 1},{Subscript[r, 2],2}}


Then, if I try to use the association defined in dict within Replace, it doesn't seem to recognize the variable.

lst /. f[x_, y_] -> dict[x]


and throws a KeyAbsent error:

{Missing["KeyAbsent", Subscript[r, 1]],
Missing["KeyAbsent", Subscript[r, 2]]}


Here's a minimal working example to reproduce the error:

Block[{dict, lst},
dict = <|Subscript[r, 1] -> 1, Subscript[r, 2] -> 2|>;
lst = f[#1[[1]], #[[2]]] & /@ {{Subscript[r, 1], 1},{Subscript[r,2], 2}};
lst /. f[x_, y_] -> dict[x]
]


Why is the output not {1,2} ?

• Hi @Szabolcs, all good points. I went a bit overboard as a result of my frustrations. I've modified the post. Aug 12, 2021 at 8:55
• Thanks for the edit, this addresses my concerns. Regarding why I removed the bugs tag: we try to add this tag only after a bug was confirmed by the community (see tag description). Aug 12, 2021 at 8:55
• Ah right ! Thanks. Just out of curiosity, how does one go about verifying that a result is a bug ? Is it posted in the chat first ? Aug 12, 2021 at 9:01
• The idea is that if you post a new question, you should not use bugs. Most of the time, you should not use it at all for your own questions. Feel free to add this tag to other people's questions if after a careful look, you have become convinced that it really is a bug (and not a misunderstanding by the asker). You can think of it as "volunteer peer review". Aug 12, 2021 at 9:03

This is not a bug. Compare the output of these two lines:

lst /. f[x_, y_] -> dict[x]


and

lst /. f[x_, y_] :> dict[x]


In the first case, dict[x] evaluates to Missing["KeyAbsent", x] and then a value is substituted for x.

In the second case, first a value such as Subscript[r,1] is substituted for x in dict[x], obtaining dict[ Subscript[r,1] ]. Evaluation proceeds afterwards.

• Perfect ! Thanks. Why is it that after 5 years of consistent MMA use, I still fall for these "trivial" oversight ? I wonder if its just my stupidity or whether this notations is unnatural. Aug 12, 2021 at 8:57
• @v1j4y The output Missing["KeyAbsent", Subscript[r, 1]] really is misleading. Consider how we always use := instead of = in function definitions, even though technically it is necessary only sometimes. I think it would make sense to default to :> in replacement rules as well, unless there is a clear reason to use ->. For some reason, we don't conventionally do this, as a community. Maybe it's time to change. Aug 12, 2021 at 9:00
• Right ! So the -> signifies: evaluate then replace and :> signifies: replace and hold right ? Similar to the = and the hold version :=. Aug 12, 2021 at 9:03