5
$\begingroup$
spA = {}; spB = {}; spC = {};

AppendTo[#, {"Up", "Down"}] & /@ {spA, spB, spC}

doesn't work in Mathematica 11.1.1.

Why? What is an alternative?

$\endgroup$
1
  • 1
    $\begingroup$ just plain Append ? $\endgroup$
    – george2079
    Commented Jun 5, 2017 at 17:33

3 Answers 3

4
$\begingroup$

It's an evaluation leak issue. You could try:

spA={}; spB={}; spC={};

Function[Null,AppendTo[#,{"Up","Down"}],HoldAll] /@ Unevaluated@{spA,spB,spC};
spA
spB
spC

{{"Up", "Down"}}

{{"Up", "Down"}}

{{"Up", "Down"}}

$\endgroup$
1
$\begingroup$
a = b = c = {};

Using Append with ApplyTo (new in 12.2)

{a, b, c} //= Map @ First @* Append[{"Up", "Down"}];

{a, b, c}

{{"Up", "Down"}, {"Up", "Down"}, {"Up", "Down"}}

$\endgroup$
1
$\begingroup$

An alternative is to use Insert:

a = b = c = {};

Flatten@Insert[{"Up", "Down"}, #, 1] & /@ {a, b, c}

(*{{"Up", "Down"}, {"Up", "Down"}, {"Up", "Down"}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.