PrependTo
PrependTo[x,elem]
prepends elem to the value of x, and resets x to the result.
Details
- PrependTo[x,elem] is equivalent to x=Prepend[x,elem].
- PrependTo has attribute HoldFirst.
- In PrependTo[x,elem], x can be a symbol or other expression with an existing value. »
- You can use PrependTo repeatedly to build up a list, though Sow and Reap will usually be more efficient. »
- PrependTo works on SparseArray objects, returning ordinary lists if necessary. »
- PrependTo works on Association objects, just like Prepend.
Examples
open allclose allBasic Examples (1)
Scope (5)
Prepend to an Association:
Add a row to the top of the matrix:
Prepending an element with inconsistent dimensions requires converting to ordinary lists:
The first argument to PrependTo need not be a symbol:
Applications (1)
Possible Issues (4)
The first argument must have a value:
The first argument must be assigned to something which can be prepended to:
Something for which AtomQ is True cannot generally be prepended to:
Using PrependTo 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, PrependTo is atomic:
The seemingly equivalent expression using Append is not atomic:
Text
Wolfram Research (1988), PrependTo, Wolfram Language function, https://reference.wolfram.com/language/ref/PrependTo.html (updated 2014).
CMS
Wolfram Language. 1988. "PrependTo." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/PrependTo.html.
APA
Wolfram Language. (1988). PrependTo. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PrependTo.html