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.

This partitions in blocks of 3.
In[1]:=
Click for copyable input
Out[1]=
This partitions in blocks of 3 with offset 1.
In[2]:=
Click for copyable input
Out[2]=
The offset can be larger than the block size.
In[3]:=
Click for copyable input
Out[3]=
This splits into runs of identical elements.
In[4]:=
Click for copyable input
Out[4]=
This splits into runs where adjacent elements are unequal.
In[5]:=
Click for copyable input
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]:=
Click for copyable input
Out[6]=
The same is true here.
In[7]:=
Click for copyable input
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]:=
Click for copyable input
Out[8]=
Now the original list is treated as being padded with the element .
In[9]:=
Click for copyable input
Out[9]=
This pads cyclically with elements and .
In[10]:=
Click for copyable input
Out[10]=
This introduces no padding, yielding sublists of differing lengths.
In[11]:=
Click for copyable input
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]:=
Click for copyable input
Out[12]=
This allows overlaps only at the beginning.
In[13]:=
Click for copyable input
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,{kL,kR}]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,{x1,x2,...}]
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.

This makes appear at position 1 in the first sublist.
In[14]:=
Click for copyable input
Out[14]=
This makes appear at position 2 in the first sublist.
In[15]:=
Click for copyable input
Out[15]=
Here is in effect made to appear first at position 4.
In[16]:=
Click for copyable input
Out[16]=
This fills in padding cyclically from the list given.
In[17]:=
Click for copyable input
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,{x1,x2,...}]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

Padding a list.

This pads the list to make it length 6.
In[18]:=
Click for copyable input
Out[18]=
This cyclically inserts as the padding.
In[19]:=
Click for copyable input
Out[19]=
This also leaves a margin of 3 on the right.
In[20]:=
Click for copyable input
Out[20]=

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

This creates a 3×3 array.
In[21]:=
Click for copyable input
Out[21]=
This partitions the array into 2×2 blocks with offset 1.
In[22]:=
Click for copyable input
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]:=
Click for copyable input
Out[23]=
Here is a list containing only padding.
In[24]:=
Click for copyable input
Out[24]=
New to Mathematica? Find your learning path »
Have a question? Ask support »