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 at level i in list.

Partition[list,n,d,{kL,kR}]
specifies that the first element of list should appear at position in the first sublist, and the last element of list should appear at or after position 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 .

Partition[list,n,d,{kL,kR},{}]
uses no padding, and so can yield sublists of different lengths.

Partition[list,nlist,dlist,{klistL,klistR},padlist]
specifies alignments and padding in a nested list.

DetailsDetails

  • 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.
  • 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. »
  • 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 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:
  • xpad with repetitions of a single element
    {x1,x2,}pad with cyclic repetitions of a sequence of elements
    listpad by treating list as cyclic (default)
    {}do 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 of list should appear at position in the block of the result, while element of list should appear at or after position in the block of the result.
  • is taken to be equivalent to .
  • is taken to be equivalent to .
  • 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 then Partition[list,{n1,n2,,nr}] will have dimensions , where is given by Floor[si/ni].
  • The object list need not have head List. »
  • Partition can be used on SparseArray objects.
Introduced in 1988
(1.0)
| Updated in 2003
(5.0)