Wolfram Research, Inc.

1.8.11 Advanced Topic: Alignment and Padding in the Partitioning of Lists

What Partition does is to pick out a sequence of sublists from a list. A subtle but important issue is where to begin the first sublist and where to end the last sublist relative to the original list. The default is simply to drop any sublists that would "overhang" on either side. In general Mathematica allows you to specify the position in the first sublist that should correspond to the first element of the original list, and the first possible position in the last sublist that should be allowed to correspond to the last element of the original list. When this specification implies that there should be overhangs, Mathematica by default fills in the necessary additional elements by treating the original list as cyclic. It also allows you instead to explicitly give a sequence of elements with which to "pad out" the list.

Alignment and padding of sublists.

Here is a list.

In[1]:= t = {a, b, c, d, e, f, g}

Out[1]=

This partitions the list, stopping before any overhang occurs.

In[2]:= Partition[t, 3, 1]

Out[2]=

The last two sublists involve overhangs. The additional elements needed are filled in by assuming that the original list is cyclic.

In[3]:= Partition[t, 3, 1, {1, 1}]

Out[3]=

This fills in additional elements by padding with x.

In[4]:= Partition[t, 3, 1, {1, 1}, x]

Out[4]=

This pads by cyclically repeating the block x, y.

In[5]:= Partition[t, 3, 1, {1, 1}, {x, y}]

Out[5]=

This has overhangs at both the beginning and end.

In[6]:= Partition[t, 3, 1, {-1, 1}, x]

Out[6]=

This uses no padding, and yields shorter lists at the beginning and end.

In[7]:= Partition[t, 3, 1, {-1, 1}, {}]

Out[7]=