Manipulating Elements of Lists

Many of the most powerful list manipulation operations in Mathematica treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list.

You can refer to an element of a Mathematica list by giving its "index". The elements are numbered in order, starting at 1.

{a,b,c}a list
Part[list,i] or list[[i]]the i^(th) element of list (the first element is list[[1]])
Part[list,{i,j,...}] or list[[{i,j,...}]]a list of the i^(th), j^(th), ... elements of list
Part[list,i;;j]a list of the i^(th) through j^(th) elements of list

Operations on list elements.

This extracts the second element of the list.
In[1]:=
Click for copyable input
Out[1]=
This extracts a list of elements.
In[2]:=
Click for copyable input
Out[2]=
This assigns the value of to be a list.
In[3]:=
Click for copyable input
Out[3]=
You can extract elements of .
In[4]:=
Click for copyable input
Out[4]=

By assigning a variable to be a list, you can use Mathematica lists much like "arrays" in other computer languages. Thus, for example, you can reset an element of a list by assigning a value to .

Part[v,i] or v[[i]]extract the i^(th) element of a list
Part[v,i]=value or v[[i]]=valuereset the i^(th) element of a list

Array-like operations on lists.

Here is a list.
In[5]:=
Click for copyable input
Out[5]=
This resets the third element of the list.
In[6]:=
Click for copyable input
Out[6]=
Now the list assigned to has been modified.
In[7]:=
Click for copyable input
Out[7]=
New to Mathematica? Find your learning path »
Have a question? Ask support »