# Adding, Removing, and Modifying List Elements

 Prepend[list,element] add element at the beginning of list Append[list,element] add element at the end of list Insert[list,element,i] insert element at position i in list Insert[list,element,-i] insert at position i counting from the end of list Riffle[list,element] interleave element between the entries of list Delete[list,i] delete the element at position i in list ReplacePart[list,i->new] replace the element at position i in list with new ReplacePart[list,{i,j}->new] replace list[[i,j]] with new

Functions for manipulating elements in explicit lists.

This gives a list with x prepended.
 In[1]:=
 Out[1]=
This inserts x so that it becomes element number 2.
 In[2]:=
 Out[2]=
This interleaves x between the entries of the list.
 In[3]:=
 Out[3]=
This replaces the third element in the list with x.
 In[4]:=
 Out[4]=
This replaces the 1, 2 element in a 2×2 matrix.
 In[5]:=
 Out[5]=

Functions like ReplacePart take explicit lists and give you new lists. Sometimes, however, you may want to modify a list "in place", without explicitly generating a new list.

 v={e1,e2,…} assign a variable to be a list v[[i]]=new assign a new value to the i element

Resetting list elements.

This defines v to be a list.
 In[6]:=
 Out[6]=
This sets the third element to be x.
 In[7]:=
 Out[7]=
Now v has been changed.
 In[8]:=
 Out[8]=
 m[[i,j]]=new replace the (i,j) element of a matrix m[[i]]=new replace the i row m[[All,i]]=new replace the i column

Resetting pieces of matrices.

This defines m to be a matrix.
 In[9]:=
 Out[9]=
This sets the first column of the matrix.
 In[10]:=
 Out[10]=
This sets every element in the first column to be 0.
 In[11]:=
 Out[11]=