PrependTo[x,elem]
prepends elem to the value of x, and resets x to the result.
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 all close allBasic Examples (1)
Scope (5)
e = f[a, b, c];
PrependTo[e, x + y]Prepend to an Association:
x = <|1 -> a, 2 -> b|>AppendTo[x, 3 -> c]PrependTo[x, 4 -> d]s = SparseArray[{{i_, i_} -> i}, 3]Add a row to the top of the matrix:
PrependTo[s, {1, 2, 3}]Prepending an element with inconsistent dimensions requires converting to ordinary lists:
PrependTo[s, {4, 5}]The first argument to PrependTo need not be a symbol:
list = {{1}, {2}, {3}}PrependTo[list[[2]], a]listAppend to a shared list in parallel:
sl = {};SetSharedVariable[sl];ParallelEvaluate[PrependTo[sl, $KernelID]];slApplications (1)
Possible Issues (4)
The first argument must have a value:
PrependTo[k, 2]The first argument must be assigned to something which can be prepended to:
k = 1;PrependTo[k, 2]Something for which AtomQ is True cannot generally be prepended to:
AtomQ[k]Using PrependTo to accumulate values in large loops can be slow:
BlockRandom[Timing[a = {};sum = 0;While[sum < 10 ^ 4, r = RandomReal[];sum += r;PrependTo[a, r]];
Length[a]]]There are many alternatives, such as using Reap and Sow:
BlockRandom[Timing[sum = 0;{r, {a}} = Reap[While[sum < 10 ^ 4, r = RandomReal[]; sum += r; Sow[r]]];a = Reverse[a];Length[a]]]When working on a shared list in parallel, PrependTo is atomic:
sl = {};SetSharedVariable[sl]ParallelDo[PrependTo[sl, i ^ 2], {i, 1, 8}]slThe seemingly equivalent expression using Append is not atomic:
sl = {};SetSharedVariable[sl]ParallelDo[sl = Prepend[sl, i ^ 2], {i, 1, 8}]slTech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2014 (10.0)
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
BibTeX
@misc{reference.wolfram_2026_prependto, author="Wolfram Research", title="{PrependTo}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/PrependTo.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_prependto, organization={Wolfram Research}, title={PrependTo}, year={2014}, url={https://reference.wolfram.com/language/ref/PrependTo.html}, note=[Accessed: 13-June-2026]}