Wolfram Language

Core Language

Generate All Partitions of a List

The new functions FoldPair and FoldPairList allow mapping simultaneously an output function and a state-update function.

Take a length-5 list to be partitioned.

In[1]:=
Click for copyable input
list = {a, b, c, d, e};

Use FoldPairList together with TakeDrop to generate a partition of the following lengths.

In[2]:=
Click for copyable input
lengths = {2, 2, 1}; FoldPairList[TakeDrop, list, lengths]
Out[2]=

To construct all partitions, find all possible decompositions of the number 5.

In[3]:=
Click for copyable input
lengthsAll = Flatten[Permutations /@ IntegerPartitions[5], 1]
Out[3]=
In[4]:=
Click for copyable input
FoldPairList[TakeDrop, list, #] & /@ lengthsAll // Column
Out[4]=

Related Examples

de es fr ja ko pt-br ru zh