# Partitioning and Padding Lists

Partition[list,n] | partition list into sublists of length n |

Partition[list,n,d] | partition into sublists with offset d |

Split[list] | split list into runs of identical elements |

Split[list,test] | split into runs with adjacent elements satisfying test |

Partitioning elements in a list.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

Partition in effect goes through a list, grouping successive elements into sublists. By default it does not include any sublists that would "overhang" the original list.

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

You can tell Partition to include sublists that overhang the ends of the original list. By default, it fills in additional elements by treating the original list as cyclic. It can also treat it as being padded with elements that you specify.

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

You can think of Partition as extracting sublists by sliding a template along and picking out elements from the original list. You can tell Partition where to start and stop this process.

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

Partition[list,n,d] or Partition[list,n,d,{1,-1}] | keep only sublists with no overhangs |

Partition[list,n,d,{1,1}] | allow an overhang at the end |

Partition[list,n,d,{-1,-1}] | allow an overhang at the beginning |

Partition[list,n,d,{-1,1}] | allow overhangs at both the beginning and end |

Partition[list,n,d,{k_{L},k_{R}}] | specify alignments of first and last sublists |

Partition[list,n,d,spec] | pad by cyclically repeating elements in list |

Partition[list,n,d,spec,x] | pad by repeating the element x |

Partition[list,n,d,spec,{x_{1},x_{2},…}] | |

pad by cyclically repeating the | |

Partition[list,n,d,spec,{}] | use no padding |

Specifying alignment and padding.

An alignment specification tells Partition to give the sequence of sublists in which the first element of the original list appears at position in the first sublist, and the last element of the original list appears at position in the last sublist.

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

Functions like ListConvolve use the same alignment and padding specifications as Partition.

In some cases it may be convenient to insert explicit padding into a list. You can do this using PadLeft and PadRight.

PadLeft[list,n] | pad to length n by inserting zeros on the left |

PadLeft[list,n,x] | pad by repeating the element x |

PadLeft[list,n,{x_{1},x_{2},…}] | pad by cyclically repeating the |

PadLeft[list,n,list] | pad by cyclically repeating list |

PadLeft[list,n,padding,m] | leave a margin of m elements on the right |

PadRight[list,n] | pad by inserting zeros on the right |

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

In[20]:= |

Out[20]= |

PadLeft, PadRight, and Partition can all be used on nested lists.

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

If you give a nested list as a padding specification, its elements are picked up cyclically at each level.

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |