Unexpected behaviour of Nothing inside List inside Association
The observed puzzle is resolved by the following two observations:
From Leonid's answer in the linked q/a:
Association
isHoldAllComplete
. Once it is created, its parts will then normally be held unevaluated.
From Nothing
>> Details
- Nothing is removed as part of the standard evaluation process. It is not removed in expressions that are held or inactive.
You can Map
ReplaceAll
on the association to force removal of Nothing
s:
ReplaceAll[d -> Nothing] /@ assoc (* or *)
Map[# /. d -> Nothing &]@<|a -> {c, d, e}|>
<|a -> {c, e}|>
Use Replace
with level spec All
rather than ReplaceAll
:
Replace[<|a -> {c, d, e}|>, d -> Nothing, All]
<|a -> {c, e}|>
Related: Is there a difference between Replace with parameter "All" and ReplaceAll
Pattern matching to a function evaluation inside an Association answers your question about bug vs expected result.
This will not do what your input suggests but let me anticipate your needs:
<|a -> {b, c, d}, e :> {d, Print[1], d}, f -> d|> //.
{a___, d, b___} :> {a, b} /. (*this way Print is not evaluated*)
d -> Nothing
<|a -> {b, c}, e :> {Print[1]}, f -> Nothing|>