Partitioning and Padding Lists

Partitioning elements in a list.

This partitions in blocks of 3:

Partition[{a,b,c,d,e,f},3]

This partitions in blocks of 3 with offset 1:

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

The offset can be larger than the block size:

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

This splits into runs of identical elements:

Split[{1,4,1,1,1,2,2,3,3}]

This splits into runs where adjacent elements are unequal:

Split[{1,4,1,1,1,2,2,3,3},Unequal]

(RefLink[Partition, paclet:ref/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:

Partition[{a,b,c,d,e},2]

The same is true here:

Partition[{a,b,c,d,e},3,1]

You can tell (RefLink[Partition, paclet:ref/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:

Partition[{a,b,c,d,e},3,1,{1,1}]

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

Partition[{a,b,c,d,e},3,1,{1,1},x]

This pads cyclically with elements x and y:

Partition[{a,b,c,d,e},3,1,{1,1},{x,y}]

This introduces no padding, yielding sublists of differing lengths:

Partition[{a,b,c,d,e},3,1,{1,1},{}]

You can think of (RefLink[Partition, paclet:ref/Partition]) as extracting sublists by sliding a template along and picking out elements from the original list. You can tell (RefLink[Partition, paclet:ref/Partition]) where to start and stop this process.

This gives all sublists that overlap the original list:

Partition[{a,b,c,d},3,1,{-1,1},x]

This allows overlaps only at the beginning:

Partition[{a,b,c,d},3,1,{-1,-1},x]

Specifying alignment and padding.

An alignment specification {(Subscript[k, L]),(Subscript[k, R])} tells (RefLink[Partition, paclet:ref/Partition]) to give the sequence of sublists in which the first element of the original list appears at position (Subscript[k, L]) in the first sublist, and the last element of the original list appears at position (Subscript[k, R]) in the last sublist.

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

Partition[{a,b,c,d},3,1,{1,1},x]

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

Partition[{a,b,c,d},3,1,{2,1},x]

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

Partition[{a,b,c,d},3,1,{4,1},x]

This fills in padding cyclically from the list given:

Partition[{a,b,c,d},3,1,{4,1},{x,y}]

Functions like (RefLink[ListConvolve, paclet:ref/ListConvolve]) use the same alignment and padding specifications as (RefLink[Partition, paclet:ref/Partition]).

In some cases it may be convenient to insert explicit padding into a list. You can do this using (RefLink[PadLeft, paclet:ref/PadLeft]) and (RefLink[PadRight, paclet:ref/PadRight]).

Padding a list.

This pads the list to make it length 6:

PadLeft[{a,b,c},6]

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

PadLeft[{a,b,c},6,{x,y}]

This also leaves a margin of 3 on the right:

PadLeft[{a,b,c},10,{x,y},3]

(RefLink[PadLeft, paclet:ref/PadLeft]), (RefLink[PadRight, paclet:ref/PadRight]), and (RefLink[Partition, paclet:ref/Partition]) can all be used on nested lists.

This creates a 3×3 array:

PadLeft[{{a,b},{e},{f}},{3,3},x]

This partitions the array into 2×2 blocks with offset 1:

Partition[%,{2,2},{1,1}]

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:

PadLeft[{{a,b},{e},{f}},{4,4},{{x,y},{z,w}}]

Here is a list containing only padding:

PadLeft[{{}},{4,4},{{x,y},{z,w}}]