Manipulating Lists by Their Indices

Part[list,spec] or list[[spec]]part or parts of a list
Part[list,spec1,spec2,] or list[[spec1,spec2,]]part or parts of a nested list
nthe n^(th) part from the beginning
-nthe n^(th) part from the end
{i1,i2,}a list of parts
m;;nparts m through n
Allall parts

Getting parts of lists.

This gives a list of parts 1 and 3.
In[1]:=
Click for copyable input
Out[1]=
Here is a nested list.
In[2]:=
Click for copyable input
This gives a list of its first and third parts.
In[3]:=
Click for copyable input
Out[3]=
This gives a list of the first part of each of these.
In[4]:=
Click for copyable input
Out[4]=
And this gives a list of the first two parts.
In[5]:=
Click for copyable input
Out[5]=
This gives the first two parts of .
In[6]:=
Click for copyable input
Out[6]=
This gives the last part of each of these.
In[7]:=
Click for copyable input
Out[7]=
This gives the second part of all sublists.
In[8]:=
Click for copyable input
Out[8]=
This gives the last two parts of all sublists.
In[9]:=
Click for copyable input
Out[9]=

You can always reset one or more pieces of a list by doing an assignment like .

This resets part 1,2 of .
In[10]:=
Click for copyable input
Out[10]=
This is now the form of .
In[11]:=
Click for copyable input
Out[11]=
This resets part 1 to and part 3 to .
In[12]:=
Click for copyable input
Out[12]=
This resets parts 1 and 3 both to .
In[13]:=
Click for copyable input
Out[13]=
This restores the original form of .
In[14]:=
Click for copyable input
This now resets all parts specified by .
In[15]:=
Click for copyable input
Out[15]=
You can use ;; to indicate all indices in a given range.
In[16]:=
Click for copyable input
Out[16]=

It is sometimes useful to think of a nested list as being laid out in space, with each element at a coordinate position given by its indices. There is then a direct geometrical interpretation for . If a given is a single integer, then it represents extracting a single slice in the k^(th) dimension, while if it is a list, it represents extracting a list of parallel slices. The final result for is then the collection of elements obtained by slicing in each successive dimension.

Here is a nested list laid out as a twodimensional array.
In[17]:=
Click for copyable input
Out[17]//TableForm=
This picks out rows 1 and 3, then columns 1 and 2.
In[18]:=
Click for copyable input
Out[18]//TableForm=

Part is set up to make it easy to pick out structured slices of nested lists. Sometimes, however, you may want to pick out arbitrary collections of individual parts. You can do this conveniently with Extract.

Part[list,{i1,i2,}]the list
Extract[list,{i1,i2,}]the element
Part[list,spec1,spec2,]parts specified by successive slicing
Extract[list,{{i1,i2,},{j1,j2,},}]the list of individual parts

Getting slices versus lists of individual parts.

This extracts the individual parts 1,3 and 1,2.
In[19]:=
Click for copyable input
Out[19]=

An important feature of Extract is that it takes lists of part positions in the same form as they are returned by functions like Position.

This sets up a nested list.
In[20]:=
Click for copyable input
This gives a list of positions in .
In[21]:=
Click for copyable input
Out[21]=
This extracts the elements at those positions.
In[22]:=
Click for copyable input
Out[22]=
Take[list,spec]take the specified parts of a list
Drop[list,spec]drop the specified parts of a list
Take[list,spec1,spec2,], Drop[list,spec1,spec2,]take or drop specified parts at each level in nested lists
nthe first n elements
-nthe last n elements
{n}element n only
{m,n}elements m through n (inclusive)
{m,n,s}elements m through n in steps of s
Allall parts
Noneno parts

Taking and dropping sequences of elements in lists.

This takes every second element starting at position 2.
In[23]:=
Click for copyable input
Out[23]=
This drops every second element.
In[24]:=
Click for copyable input
Out[24]=

Much like Part, Take and Drop can be viewed as picking out sequences of slices at successive levels in a nested list, you can use Take and Drop to work with blocks of elements in arrays.

Here is a 3×3 array.
In[25]:=
Click for copyable input
Out[25]//TableForm=
Here is the first 2×2 subarray.
In[26]:=
Click for copyable input
Out[26]//TableForm=
This takes all elements in the first two columns.
In[27]:=
Click for copyable input
Out[27]//TableForm=
This leaves no elements from the first two columns.
In[28]:=
Click for copyable input
Out[28]//TableForm=
Prepend[list,elem]add element at the beginning of list
Append[list,elem]add element at the end of list
Insert[list,elem,i]insert element at position i
Insert[list,elem,{i,j,}]insert at position
Delete[list,i]delete the element at position i
Delete[list,{i,j,}]delete at position

Adding and deleting elements in lists.

This makes the 2,1 element of the list be .
In[29]:=
Click for copyable input
Out[29]=
This deletes the element again.
In[30]:=
Click for copyable input
Out[30]=
ReplacePart[list,i->new]replace the element at position i in list with new
ReplacePart[list,{i,j,}->new]replace with new
ReplacePart[list,{i1->new1,i2->new2,}]replaces parts at positions by
ReplacePart[list,{{i1,j1,}->new1,}]replace parts at positions by
ReplacePart[list,{{i1,j1,},}->new]replace all parts with new

Replacing parts of lists.

This replaces the third element in the list with .
In[31]:=
Click for copyable input
Out[31]=
This replaces the first and fourth parts of the list. Notice the need for double lists in specifying multiple parts to replace.
In[32]:=
Click for copyable input
Out[32]=
Here is a 3×3 identity matrix.
In[33]:=
Click for copyable input
Out[33]=
This replaces the 2,2 component of the matrix by .
In[34]:=
Click for copyable input
Out[34]=

It is important to understand that ReplacePart always creates a new list. It does not modify a list that has already been assigned to a symbol the way does.

This assigns a list of values to .
In[35]:=
Click for copyable input
Out[35]=
This gives a copy of the list in which the third element has been replaced with .
In[36]:=
Click for copyable input
Out[36]=
The value of has not changed.
In[37]:=
Click for copyable input
Out[37]=