Manipulating Lists by Their Indices

Getting parts of lists.

This gives a list of parts 1 and 3:

{a,b,c,d}[[{1,3}]]

Here is a nested list:

m={{a,b,c},{d,e},{f,g,h}};

This gives a list of its first and third parts:

m[[{1,3}]]

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

m[[{1,3},1]]

And this gives a list of the first two parts:

m[[{1,3},{1,2}]]

This gives the first two parts of m:

m[[1;;2]]

This gives the last part of each of these:

m[[1;;2,-1]]

This gives the second part of all sublists:

m[[All,2]]

This gives the last two parts of all sublists:

m[[All,-2;;-1]]

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:

m[[1,2]]=x

This is now the form of m:

m

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

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

This resets parts 1 and 3 both to p:

m[[{1,3}]]=p;m

This restores the original form of m:

m={{a,b,c},{d,e},{f,g,h}};

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

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

You can use (RefLink[;;, paclet:ref/Span]) to indicate all indices in a given range:

m[[1;;3,2]]=y;m

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 list[[(Subscript[spec, 1]),(Subscript[spec, 2]),…]]. If a given (Subscript[spec, k]) 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 list[[(Subscript[spec, 1]),(Subscript[spec, 2]),…]] 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:

(m={{a,b,c},{d,e,f},{g,h,i}})//TableForm

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

m[[{1,3},{1,2}]]//TableForm

(RefLink[Part, paclet:ref/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 (RefLink[Extract, paclet:ref/Extract]).

Getting slices versus lists of individual parts.

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

Extract[m,{{1,3},{1,2}}]

An important feature of (RefLink[Extract, paclet:ref/Extract]) is that it takes lists of part positions in the same form as they are returned by functions like (RefLink[Position, paclet:ref/Position]).

This sets up a nested list:

m={{a[1],a[2],b[1]},{b[2],c[1]},{{b[3]}}};

This gives a list of positions in m:

Position[m,b[_]]

This extracts the elements at those positions:

Extract[m,%]

Taking and dropping sequences of elements in lists.

This takes every second element starting at position 2:

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

This drops every second element:

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

Much like (RefLink[Part, paclet:ref/Part]), (RefLink[Take, paclet:ref/Take]) and (RefLink[Drop, paclet:ref/Drop]) can be viewed as picking out sequences of slices at successive levels in a nested list, you can use (RefLink[Take, paclet:ref/Take]) and (RefLink[Drop, paclet:ref/Drop]) to work with blocks of elements in arrays.

Here is a 3×3 array:

(m={{a,b,c},{d,e,f},{g,h,i}})//TableForm

Here is the first 2×2 subarray:

Take[m,2,2]//TableForm

This takes all elements in the first two columns:

Take[m,All,2]//TableForm

This leaves no elements from the first two columns:

Drop[m,None,2]//TableForm

Adding and deleting elements in lists.

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

Insert[{{a,b,c},{d,e}},x,{2,1}]

This deletes the element again:

Delete[%,{2,1}]

Replacing parts of lists.

This replaces the third element in the list with x:

ReplacePart[{a,b,c,d},3->x]

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

ReplacePart[{a,b,c,d},{{1},{4}}->x]

Here is a 3×3 identity matrix:

IdentityMatrix[3]

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

ReplacePart[%,{2,2}->x]

It is important to understand that (RefLink[ReplacePart, paclet:ref/ReplacePart]) always creates a new list. It does not modify a list that has already been assigned to a symbol the way m[[…]]=val does.

This assigns a list of values to alist:

alist={a,b,c,d}

This gives a copy of the list in which the third element has been replaced with x:

ReplacePart[alist,3->x]

The value of alist has not changed:

alist