Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Defining Variables and Functions > Assignments > PrependTo >

PrependTo

PrependTo
prepends elem to the value of s, and resets s to the result.
  • You can use PrependTo repeatedly to build up a list, though Sow and Reap will usually be more efficient. »
Assign l to a list:
Include a new value at the beginning of the list:
The value assigned to l has changed:
Assign l to a list:
In[1]:=
Click for copyable input
Out[1]=
Include a new value at the beginning of the list:
In[2]:=
Click for copyable input
Out[2]=
The value assigned to l has changed:
In[3]:=
Click for copyable input
Out[3]=
Use a head other than List:
This assigns s to a sparse matrix:
Add a row to the top of the matrix:
Prepending an element with inconsistent dimensions requires converting to ordinary lists:
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:
The first argument must be a variable:
The first argument must be a variable with a value:
The first argument must be assigned to something which can be prepended to:
Something for whichAtomQ is True cannot be prepended to:
Using PrependTo to accumulate values in large loops can be slow:
There are many alternatives, such as using Reap and Sow:
New in 1 | Last modified in 5
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF