This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Manipulating Lists /

2.4.4 Partitioning and Padding Lists

Partitioning elements in a list.

This partitions in blocks of 3.

In[1]:= Partition[{a, b, c, d, e, f}, 3]

Out[1]=

This partitions in blocks of 3 with offset 1.

In[2]:= Partition[{a, b, c, d, e, f}, 3, 1]

Out[2]=

The offset can be larger than the block size.

In[3]:= Partition[{a, b, c, d, e, f}, 2, 3]

Out[3]=

This splits into runs of identical elements.

In[4]:= Split[{1, 4, 1, 1, 1, 2, 2, 3, 3}]

Out[4]=

This splits into runs where adjacent elements are unequal.

In[5]:= Split[{1, 4, 1, 1, 1, 2, 2, 3, 3}, Unequal]

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.

This stops before any overhang occurs.

In[6]:= Partition[{a, b, c, d, e}, 2]

Out[6]=

The same is true here.

In[7]:= Partition[{a, b, c, d, e}, 3, 1]

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.

This includes additional sublists, treating the original list as cyclic.

In[8]:= Partition[{a, b, c, d, e}, 3, 1, {1, 1}]

Out[8]=

Now the original list is treated as being padded with the element x.

In[9]:= Partition[{a, b, c, d, e}, 3, 1, {1, 1}, x]

Out[9]=

This pads cyclically with elements x and y.

In[10]:= Partition[{a, b, c, d, e}, 3, 1, {1, 1}, {x, y}]

Out[10]=

This introduces no padding, yielding sublists of differing lengths.

In[11]:= Partition[{a, b, c, d, e}, 3, 1, {1, 1}, {}]

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.

This gives all sublists that overlap the original list.

In[12]:= Partition[{a, b, c, d}, 3, 1, {-1, 1}, x]

Out[12]=

This allows overlaps only at the beginning.

In[13]:= Partition[{a, b, c, d}, 3, 1, {-1, -1}, x]

Out[13]=

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.

This makes a appear at position 1 in the first sublist.

In[14]:= Partition[{a, b, c, d}, 3, 1, {1, 1}, x]

Out[14]=

This makes a appear at position 2 in the first sublist.

In[15]:= Partition[{a, b, c, d}, 3, 1, {2, 1}, x]

Out[15]=

Here a is in effect made to appear first at position 4.

In[16]:= Partition[{a, b, c, d}, 3, 1, {4, 1}, x]

Out[16]=

This fills in padding cyclically from the list given.

In[17]:= Partition[{a, b, c, d}, 3, 1, {4, 1}, {x, y}]

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.

Padding a list.

This pads the list to make it length 6.

In[18]:= PadLeft[{a, b, c}, 6]

Out[18]=

This cyclically inserts {x, y} as the padding.

In[19]:= PadLeft[{a, b, c}, 6, {x, y}]

Out[19]=

This also leaves a margin of 3 on the right.

In[20]:= PadLeft[{a, b, c}, 10, {x, y}, 3]

Out[20]=

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

This creates a array.

In[21]:= PadLeft[{{a, b}, {e}, {f}}, {3, 3}, x]

Out[21]=

This partitions the array into blocks with offset 1.

In[22]:= Partition[%, {2, 2}, {1, 1}]

Out[22]=

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

This cyclically fills in copies of the padding list.

In[23]:= PadLeft[{{a, b}, {e}, {f}}, {4, 4}, {{x, y}, {z, w}}]

Out[23]=

Here is a list containing only padding.

In[24]:= PadLeft[{{}}, {4, 4}, {{x, y}, {z, w}}]

Out[24]=