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 n_(1)×n_(2)×.

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.

Partition[list,nlist,dlist,{klistL,klistR},padlist]

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:
  • 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)
    {}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  (4)

Partition into sublists of length 2:

Partition into sublists of length 3 with offset 1:

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

Allow the last list to be shorter than the others:

Scope  (18)

Padding  (7)

Incomplete sublists at the end are by default dropped:

Allow for a "ragged" partition, wherein last elements can be shorter:

Treat the list as cyclic:

Pad with x:

Pad by repeating {x,y,z} blocks:

Use no padding, so later sublists can be shorter:

Pad with Nothing to achieve the same result:

Partition from the end and use no padding:

Padding Alignment  (6)

Insert padding at the end:

Insert padding at the beginning:

Insert padding at both ends:

Insert no padding:

Make the input start at position 2 in the first sublist, and end at position 1 in the last sublist:

Pad by laying down repeating copies of {x,y}:

Multidimensional Arrays  (5)

Partition into 2×2 blocks:

Partition into 2×2 blocks with offset 1, padding at the ends of rows and columns with xx:

Partition into 2×2 blocks with offset 1, taking the array to be cyclic in both dimensions:

Insert padding at the beginnings of columns and the beginnings and ends of rows:

Pad the beginnings and ends of columns and the ends of rows:

Generalizations & Extensions  (4)

Skip elements by using an offset larger than the block size:

Padding can be longer than the block size:

The head need not be List:

Partition works with SparseArray objects:

Applications  (5)

Partition to lay out a list in 2D:

Find pairs of successive elements in a list:

Find successive ratios in a list:

Implement an elementary cellular automaton:

Partition a flat list of elements into a multidimensional array with specified dimensions:

Create a multidimensional array of successive integers:

Restructure a matrix:

Properties & Relations  (3)

Flatten is essentially the inverse of Partition:

The sublists in the result of Partition have the same length:

UpTo allows having a shorter list at the end:

TakeList allows specifying the lengths of the sublists:

ListCorrelate[ker,list] effectively combines ker with a partitioned list:

Partition the list into segments the length of ker with offset 1:

Use Dot to combine the kernel with each segment:

ListConvolve is similar except the kernel is reversed:

Wolfram Research (1988), Partition, Wolfram Language function, https://reference.wolfram.com/language/ref/Partition.html (updated 2015).

Text

Wolfram Research (1988), Partition, Wolfram Language function, https://reference.wolfram.com/language/ref/Partition.html (updated 2015).

CMS

Wolfram Language. 1988. "Partition." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/Partition.html.

APA

Wolfram Language. (1988). Partition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Partition.html

BibTeX

@misc{reference.wolfram_2023_partition, author="Wolfram Research", title="{Partition}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/Partition.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_partition, organization={Wolfram Research}, title={Partition}, year={2015}, url={https://reference.wolfram.com/language/ref/Partition.html}, note=[Accessed: 19-March-2024 ]}