# How to dynamically track changes to an Association

Bug introduced in 10.4 or earlier and persisting through 11.1.1

CASE:3892974

Thanks for bringing this to our attention. A report has been filed with the developers.

I can track changes to a list with Dynamic without too much difficulty

foolist = {};
Dynamic[foolist]


And if I were to append an element to foolist, the dynamic expression will refresh to {list}

AppendTo[foolist, list]


But when I do the same with an association,

foobar = <||>;
Dynamic[foobar]


And add a new entry to it

AssociateTo[foobar, foo->bar]


The dynamic expression won't be able to capture the change. Am I doing something wrong?

• I don't think you do anything wrong, unless I'm missing something I think you should report this. p.s. notice that you can still do AppendTo[foobar, foo -> bar] and it will work well. – Kuba May 18 '17 at 18:40
• @Kuba Ah thanks! And it seems that all Set assignments to an association can be tracked as well – arax May 18 '17 at 20:50
• Indeed. I've reported that, will let you know in case of any news. (CASE:3892974) – Kuba May 18 '17 at 21:38

Meanwhile you can use:

AppendTo[foobar, foo->bar]


or more functional style:

foobar = <|foobar, foo->bar|>;


foobar = Append[foobar, foo->bar];

• Won't the result of the "more idiomatic" be an undesirable <|<||>, foo->bar|>? – LLlAMnYP May 20 '17 at 15:27
• @LLlAMnYP I don't understand, have you tried? But maybe I shoud've said "more functional style". – Kuba May 20 '17 at 16:40
• since it's the weekend, no, I haven't. Are you saying, the first element won't be the empty association that foobar was before assignment? Since for, say, a list saying foobar={}; foobar={foobar, foo->bar} will give {{}, foo->bar} and not {foo->bar} – LLlAMnYP May 21 '17 at 17:03
• @LLlAMnYP <|assoc1, assoc2|> works like Merge[{assoc1, assoc2}, Last] – Kuba May 21 '17 at 17:36
• Interesting, so it autoflattens. – LLlAMnYP May 21 '17 at 17:37