# Partition

Partition[list,n]

partitions list into nonoverlapping sublists of length n.

Partition[list,n,d]

generates sublists with offset d.

Partition[list,{n1,n2,}]

partitions a nested list into blocks of size ××.

Partition[list,{n1,n2,},{d1,d2,}]

uses offset di at level i in list.

Partition[list,n,d,{kL,kR}]

specifies that the first element of list should appear at position kL in the first sublist, and the last element of list should appear at or after position kR in the last sublist. If additional elements are needed, Partition fills them in by treating list as cyclic.

Partition[list,n,d,{kL,kR},x]

pads if necessary by repeating the element x.

Partition[list,n,d,{kL,kR},{x1,x2,}]

pads if necessary by cyclically repeating the elements xi.

Partition[list,n,d,{kL,kR},{}]

uses no padding, and so can yield sublists of different lengths.

specifies alignments and padding in a nested list.

# Details

• All the sublists generated by Partition[list,n,d] are of length n. Some elements at the end of list may therefore not appear in any sublist.
• Partition[list,UpTo[n]] partitions into sublists of length up to n, allowing the final sublist to be shorter.
• All elements of list appear in the sublists generated by Partition[list,n,1].
• If d is greater than n in Partition[list,n,d], then elements in the middle of list are skipped. »
• Sublist lengths can always be specified as UpTo[n], in which case shorter sublists can be returned if fewer than n elements are available.
• Partition[list,n,d,{kL,kR}] effectively allows sublists that have overhangs that extend past the beginning or end of list.
• Partition[list,n,d,k] is equivalent to Partition[list,n,d,{k,k}].
• Common settings for {kL,kR} are:
•  {1,-1} allow no overhangs {1,1} allow maximal overhang at the end {-1,-1} allow maximal overhang at the beginning {-1,1} allow maximal overhangs at both beginning and end
• Partition[list,n,d,{kL,kR},padlist] effectively lays down repeated copies of padlist, then superimposes one copy of list on them and partitions the result. »
• Common settings for padlist are:
•  x pad with repetitions of a single element {x1,x2,…} pad with cyclic repetitions of a sequence of elements list pad by treating list as cyclic (default) {} no padding, potentially leaving sublists of different lengths
• If list has length s, then Partition[list,n,d] yields Max[0,Floor[(s+d-n)/d]] sublists.
• Partition[list,{n1,n2,,nr}] effectively replaces blocks of elements at level r in list by depth-r nested lists of neighboring elements. »
• If no offsets are specified, the neighborhoods are adjacent and nonoverlapping.
• Partition[list,{n1,n2,},d] uses offset d at every level.
• Partition[list,nlist,dlist,{{kL 1,kL 2,},{kR 1,kR 2,}}] specifies that element {1,1,} of list should appear at position {kL 1,kL 2,} in the {1,1,} block of the result, while element {-1,-1,} of list should appear at or after position {kR 1,kR 2,} in the {-1,-1,} block of the result.
• {kL,kR} is taken to be equivalent to {{kL,kL,},{kR,kR,}}.
• {{k1,k2,}} is taken to be equivalent to {{k1,k2,},{k1,k2,}}.
• Partition[list,{n1,n2,,nr},klist,padlist] effectively makes a depth-r array of copies of padlist, then superimposes list on them and partitions the result.
• If list has dimensions {s1,s2,,sr}, then Partition[list,{n1,n2,,nr}] will have dimensions {q1,q2,,qr,n1,n2,,nr}, where qi is given by Floor[si/ni].
• The object list need not have head List. »
• Partition can be used on SparseArray objects.

# Examples

open allclose all

## Basic Examples(3)

Partition into sublists of length 2:

 In[1]:=
 Out[1]=

Partition into sublists of length 3 with offset 1:

 In[1]:=
 Out[1]=

Partition a two-dimensional array into 2×2 blocks with offset 1:

 In[1]:=
 Out[1]=