This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Practical Introduction / Lists  /

1.8.6 Adding, Removing and Modifying List Elements


Functions for adding and deleting elements in lists.

  • This gives a list with x prepended.
  • In[1]:= Prepend[{a, b, c}, x]

    Out[1]=

  • This adds x at the end.
  • In[2]:= Append[{a, b, c}, x]

    Out[2]=

  • This inserts x so that it becomes element number 2.
  • In[3]:= Insert[{a, b, c}, x, 2]

    Out[3]=

  • Negative numbers count from the end of the list.
  • In[4]:= Insert[{a, b, c}, x, -2]

    Out[4]=

  • Delete removes an element from the list.
  • In[5]:= Delete[{a, b, c, d}, 3]

    Out[5]=


    Modifying parts of lists.

  • This replaces the third element in the list with x.
  • In[6]:= ReplacePart[{a, b, c, d}, x, 3]

    Out[6]=

  • This replaces the first and fourth parts of the list. Notice the need for double lists in specifying multiple parts to replace.
  • In[7]:= ReplacePart[{a, b, c, d}, x, {{1}, {4}}]

    Out[7]=




  • Here is a


    identity matrix.
  • In[8]:= IdentityMatrix[3]

    Out[8]=




  • This replaces the component of the matrix by x


    .
  • In[9]:= ReplacePart[%, x, {2, 2}]

    Out[9]=