AppendTo[x,elem]
appends elem to the value of x, and resets x to the result.
AppendTo
AppendTo[x,elem]
appends elem to the value of x, and resets x to the result.
Details
- AppendTo[x,elem] is equivalent to x=Append[x,elem].
- AppendTo has attribute HoldFirst.
- In AppendTo[x,elem], x can be a symbol or other expression with an existing value. »
- You can use AppendTo repeatedly to build up a list, though Sow and Reap will usually be more efficient. »
- AppendTo works on SparseArray objects, returning ordinary lists if necessary. »
- AppendTo works on Association objects, just like Append.
Examples
open all close allBasic Examples (1)
Scope (5)
e = f[a, b, c];
AppendTo[e, x + y]Append to an Association:
x = <|1 -> a, 2 -> b|>AppendTo[x, 3 -> c]PrependTo[x, 4 -> d]s = SparseArray[{{i_, i_} -> i}, 3]AppendTo[s, {1, 2, 3}]Appending an element with inconsistent dimensions requires converting to ordinary lists:
AppendTo[s, {4, 5}]The first argument to AppendTo need not be a symbol:
list = {{1}, {2}, {3}}AppendTo[list[[2]], a]listAppend to a shared list in parallel:
sl = {};SetSharedVariable[sl]ParallelEvaluate[AppendTo[sl, $KernelID]];slApplications (1)
Possible Issues (5)
The first argument must have a value:
AppendTo[k, 2]The first argument must be assigned to something which can be appended to:
k = 1;AppendTo[k, 2]Something for which AtomQ is True cannot generally be appended to:
AtomQ[k]Using AppendTo on a column of a matrix replaces column values:
m = {{11, 12, 13}, {21, 22, 23}};AppendTo[m[[All, 1]], 4];
mm = {{11, 12, 13}, {21, 22, 23}};
val = Append[m[[All, 1]], 4]Reset the column values with the new value:
m[[All, 1]] = val;
mUsing AppendTo to accumulate values in large loops can be slow:
BlockRandom[Timing[a = {};sum = 0;While[sum < 10 ^ 5, r = RandomInteger[9];sum += r;AppendTo[a, r]];
Length[a]]]There are many alternatives, such as using Reap and Sow:
BlockRandom[Timing[sum = 0;{r, {a}} = Reap[While[sum < 10 ^ 5, r = RandomInteger[9]; sum += r; Sow[r]]];Length[a]]]When working on a shared list in parallel, AppendTo is atomic:
sl = {};SetSharedVariable[sl]ParallelDo[AppendTo[sl, i ^ 2], {i, 1, 8}]slThe seemingly equivalent expression using Append is not atomic:
sl = {};SetSharedVariable[sl]ParallelDo[sl = Append[sl, i ^ 2], {i, 1, 8}]slTech Notes
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2014 (10.0)
Text
Wolfram Research (1988), AppendTo, Wolfram Language function, https://reference.wolfram.com/language/ref/AppendTo.html (updated 2014).
CMS
Wolfram Language. 1988. "AppendTo." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/AppendTo.html.
APA
Wolfram Language. (1988). AppendTo. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/AppendTo.html
BibTeX
@misc{reference.wolfram_2026_appendto, author="Wolfram Research", title="{AppendTo}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/AppendTo.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_appendto, organization={Wolfram Research}, title={AppendTo}, year={2014}, url={https://reference.wolfram.com/language/ref/AppendTo.html}, note=[Accessed: 13-June-2026]}