PrependTo

PrependTo[x,elem]

prepends elem to the value of x, and resets x to the result.

Details

Examples

open allclose all

Basic Examples  (1)

Assign l to a list:

Include a new value at the beginning of the list:

The value assigned to l has changed:

Scope  (5)

Use an arbitrary head:

Prepend to an Association:

Assign to s a sparse matrix:

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:

Append to a shared list in parallel:

Applications  (1)

Assign m to be a 2×2 matrix:

Prepend a row to m:

Prepend a column to m:

m is now a 3×3 matrix:

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:

Wolfram Research (1988), PrependTo, Wolfram Language function, https://reference.wolfram.com/language/ref/PrependTo.html (updated 2014).

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_2024_prependto, author="Wolfram Research", title="{PrependTo}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/PrependTo.html}", note=[Accessed: 12-October-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_prependto, organization={Wolfram Research}, title={PrependTo}, year={2014}, url={https://reference.wolfram.com/language/ref/PrependTo.html}, note=[Accessed: 12-October-2024 ]}