# Manipulating Lists by Their Indices

Part[list,spec] or list[[spec]] | part or parts of a list |

Part[list,spec_{1},spec_{2},…] or list[[spec_{1},spec_{2},…]] | part or parts of a nested list |

n | the n part from the beginning |

-n | the n part from the end |

{i_{1},i_{2},…} | a list of parts |

m;;n | parts m through n |

All | all parts |

In[1]:= |

Out[1]= |

In[2]:= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

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

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

In[15]:= |

Out[15]= |

In[16]:= |

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

In[17]:= |

Out[17]//TableForm= | |

In[18]:= |

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,{i_{1},i_{2},…}] | the list |

Extract[list,{i_{1},i_{2},…}] | the element |

Part[list,spec_{1},spec_{2},…] | parts specified by successive slicing |

Extract[list,{{i_{1},i_{2},…},{j_{1},j_{2},…},…}] | the list of individual parts |

Getting slices versus lists of individual parts.

In[19]:= |

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.

In[20]:= |

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

Take[list,spec] | take the specified parts of a list |

Drop[list,spec] | drop the specified parts of a list |

Take[list,spec_{1},spec_{2},…], Drop[list,spec_{1},spec_{2},…] | 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.

In[23]:= |

Out[23]= |

In[24]:= |

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.

In[25]:= |

Out[25]//TableForm= | |

In[26]:= |

Out[26]//TableForm= | |

In[27]:= |

Out[27]//TableForm= | |

In[28]:= |

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.

In[29]:= |

Out[29]= |

In[30]:= |

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,{i_{1}->new_{1},i_{2}->new_{2},…}] | replaces parts at positions by |

ReplacePart[list,{{i_{1},j_{1},…}->new_{1},…}] | replace parts at positions by |

ReplacePart[list,{{i_{1},j_{1},…},…}->new] | replace all parts with new |

In[31]:= |

Out[31]= |

In[32]:= |

Out[32]= |

In[33]:= |

Out[33]= |

In[34]:= |

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.

In[35]:= |

Out[35]= |

In[36]:= |

Out[36]= |

In[37]:= |

Out[37]= |