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 allclose allBasic Examples (1)
Scope (5)
Append to an Association:
Appending an element with inconsistent dimensions requires converting to ordinary lists:
The first argument to AppendTo need not be a symbol:
Applications (1)
Possible Issues (5)
The first argument must have a value:
The first argument must be assigned to something which can be appended to:
Something for which AtomQ is True cannot generally be appended to:
Using AppendTo on a column of a matrix replaces column values:
Reset the column values with the new value:
Using AppendTo to accumulate values in large loops can be slow:
There are many alternatives, such as using Reap and Sow:
When working on a shared list in parallel, AppendTo is atomic:
The seemingly equivalent expression using Append is not atomic:
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