This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Manipulating Lists /

2.4.2 Manipulating Lists by Their Indices

Getting parts of lists.

This gives a list of parts 1 and 3.

In[1]:= {a, b, c, d}[[{1, 3}]]

Out[1]=

Here is a nested list.

In[2]:= m = {{a, b, c}, {d, e}, {f, g, h}};

This gives a list of its first and third parts.

In[3]:= m[[{1, 3}]]

Out[3]=

This gives a list of the first part of each of these.

In[4]:= m[[{1, 3}, 1]]

Out[4]=

And this gives a list of the first two parts.

In[5]:= m[[{1, 3}, {1, 2}]]

Out[5]=

This gives the second part of all sublists.

In[6]:= m[[All, 2]]

Out[6]=

You can always reset one or more pieces of a list by doing an assignment like m[[... ]] = value.

This resets part 1,2 of m.

In[7]:= m[[1, 2]] = x

Out[7]=

This is now the form of m.

In[8]:= m

Out[8]=

This resets part 1 to x and part 3 to y.

In[9]:= m[[{1, 3}]] = {x, y}; m

Out[9]=

This resets parts 1 and 3 both to p.

In[10]:= m[[{1, 3}]] = p; m

Out[10]=

This restores the original form of m.

In[11]:= m = {{a, b, c}, {d, e}, {f, g, h}};

This now resets all parts specified by m[[{1, 3}, {1, 2}]].

In[12]:= m[[{1, 3}, {1, 2}]] = x; m

Out[12]=

You can use Range to indicate all indices in a given range.

In[13]:= m[[Range[1, 3], 2]] = y; m

Out[13]=

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

Here is a nested list laid out as a two-dimensional array.

In[14]:= (m = {{a, b, c}, {d, e, f}, {g, h, i}}) // TableForm

Out[14]//TableForm=

This picks out rows 1 and 3, then columns 1 and 2.

In[15]:= m[[{1, 3}, {1, 2}]] // TableForm

Out[15]//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.

Getting slices versus lists of individual parts.

This extracts the individual parts 1,3 and 1,2.

In[16]:= Extract[m, {{1, 3}, {1, 2}}]

Out[16]=

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[17]:= m = {{a[1], a[2], b[1]}, {b[2], c[1]}, {{b[3]}}};

This gives a list of positions in m.

In[18]:= Position[m, b[_]]

Out[18]=

This extracts the elements at those positions.

In[19]:= Extract[m, %]

Out[19]=

Taking and dropping sequences of elements in lists.

This takes every second element starting at position 2.

In[20]:= Take[{a, b, c, d, e, f, g}, {2, -1, 2}]

Out[20]=

This drops every second element.

In[21]:= Drop[{a, b, c, d, e, f, g}, {2, -1, 2}]

Out[21]=

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 array.

In[22]:= (m = {{a, b, c}, {d, e, f}, {g, h, i}}) // TableForm

Out[22]//TableForm=

Here is the first subarray.

In[23]:= Take[m, 2, 2] // TableForm

Out[23]//TableForm=

This takes all elements in the first two columns.

In[24]:= Take[m, All, 2] // TableForm

Out[24]//TableForm=

This leaves no elements from the first two columns.

In[25]:= Drop[m, None, 2] // TableForm

Out[25]//TableForm=

Adding and deleting elements in lists.

This makes the 2,1 element of the list be x.

In[26]:= Insert[{{a, b, c}, {d, e}}, x, {2, 1}]

Out[26]=

This deletes the element again.

In[27]:= Delete[%, {2, 1}]

Out[27]=

Replacing parts of lists.

This replaces the third element in the list with x.

In[28]:= ReplacePart[{a, b, c, d}, x, 3]

Out[28]=

This replaces the first and fourth parts of the list. Notice the need for double lists in specifying multiple parts to replace.

In[29]:= ReplacePart[{a, b, c, d}, x, {{1}, {4}}]

Out[29]=

Here is a identity matrix.

In[30]:= IdentityMatrix[3]

Out[30]=

This replaces the 2,2 component of the matrix by x.

In[31]:= ReplacePart[%, x, {2, 2}]

Out[31]=