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 >

AppendTo

AppendTo[s, elem]
appends elem to the value of s, and resets s to the result.
  • You can use AppendTo 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 end 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 end 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 matrix:
Appending an element with inconsistent dimensions requires converting to ordinary lists:
Assign m to be a 2×2 matrix:
Append a row to m:
Append 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 appended to:
Something for which AtomQ is True cannot be appended to:
Using AppendTo 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