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 |
n | the n part from the beginning |
-n | the n part from the end |
{i1,i2,…} | a list of parts |
m;;n | parts m through n |
All | all parts |
In[2]:= |
You can always reset one or more pieces of a list by doing an assignment like m[[…]]=value.
In[14]:= |
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[[spec1,spec2,…]]. If a given speck 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[[spec1,spec2,…]] is then the collection of elements obtained by slicing in each successive dimension.
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 {list[[i1]],list[[i2]],…} |
Extract[list,{i1,i2,…}] | the element list[[i1,i2,…]] |
Part[list,spec1,spec2,…] | parts specified by successive slicing |
Extract[list,{{i1,i2,…},{j1,j2,…},…}] | the list of individual parts {list[[i1,i2,…]],list[[j1,j2,…]],…} |
Getting slices versus lists of individual parts.
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.
In[20]:= |
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 |
n | the first n elements |
-n | the 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 |
All | all parts |
None | no parts |
Taking and dropping sequences of elements in lists.
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.
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 {i,j,…} |
Delete[list,i] | delete the element at position i |
Delete[list,{i,j,…}] | delete at position {i,j,…} |
Adding and deleting elements in lists.
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 |
ReplacePart[list,{i1->new1,i2->new2,…}] | replaces parts at positions in by newn |
ReplacePart[list,{{i1,j1,…}->new1,…}] | replace parts at positions {in,jn,…} by newn |
ReplacePart[list,{{i1,j1,…},…}->new] | replace all parts list[[ik,jk,…]] with new |
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 m[[…]]=val does.