Partition

Lots of users are not familiar with the Partition function.  Partition is  well documented under Built-in Functions in the Help Browser.  The usage message for this powerful feature is shown below.

? Partition

Partition[list, n] partitions list into non-overlapping   sublists of length n. Partition[ ... t, {klistL,   klistR}, padlist] specifies alignments and padding in a nested list. More…


A simple demonstration of Partition is given in the next cell.  If the third
argument is not provided the result will be the same as if the a third
argument equal to the second was provided.

lst = Range[50] ;    Partition[lst, 4]

{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, ... 27, 28}, {29, 30, 31, 32}, {33, 34, 35, 36}, {37, 38, 39, 40}, {41, 42, 43, 44}, {45, 46, 47, 48}}


The effect of the third argument is demonstrated in the next two examples..

Partition[lst, 4, 3]

{{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 10}, {10, 11, 12, 13}, {13, 14, 15, 16}, {16, 17, 18, 1 ... 33, 34}, {34, 35, 36, 37}, {37, 38, 39, 40}, {40, 41, 42, 43}, {43, 44, 45, 46}, {46, 47, 48, 49}}

Partition[lst, 4, 10]

{{1, 2, 3, 4}, {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34}, {41, 42, 43, 44}}


Next we give Partition a fourth argument of 3.  Partition then treats the
list as if it were periodic and ensures the first element of (lst) is the
third element of the first sublist.

Partition[lst, 5, 5, 3]

{{49, 50, 1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10, 11, 12, 13}, {14, 15, 16, 17, 18}, {19, 20, 21, 2 ... , 27, 28}, {29, 30, 31, 32, 33}, {34, 35, 36, 37, 38}, {39, 40, 41, 42, 43}, {44, 45, 46, 47, 48}}


Next the fourth argument of Partition is 7 and we get a partition where the
first element of (lst) would be the seventh element of the result if it were
flattened.

Partition[lst, 5, 5, 7]

{{45, 46, 47, 48, 49}, {50, 1, 2, 3, 4}, {5, 6, 7, 8, 9}, {10, 11, 12, 13, 14}, {15, 16, 17, 1 ... , 23, 24}, {25, 26, 27, 28, 29}, {30, 31, 32, 33, 34}, {35, 36, 37, 38, 39}, {40, 41, 42, 43, 44}}


Partition can work in higher dimensions. In the next example Partition
seperates a matrix into blocks.

Clear["Global`*"] ;    mtrx = { {w1, w2, w3, w4, w5, w6},  {x1,  ... x3, x4, x5, x6},  {y1, y2, y3, y4, y5, y6},  {z1, z2, z3, z4, z5, z6} } ;

Partition[mtrx, {2, 2}, {2, 2}]//MatrixForm

( ( w1   w2 )   ( w3   w4 )   ( w5   w6 ᡝ ... )                      z1   z2                       z3   z4                       z5   z6


The next example demonstrates that Partition can work on expressions that
don't have the head list.

ClearAll[f] ;    flst = f @@ Range[1, 20]

f[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Partition[flst, 3]

f[f[1, 2, 3], f[4, 5, 6], f[7, 8, 9], f[10, 11, 12], f[13, 14, 15], f[16, 17, 18]]


I haven't been able to find ways to clearly demonstrate use of the Partition
features listed below.  Each of these features are in the Partition usage
message.



•  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.



•  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.


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page