How to split a list into n groups in all possible combinations of group length and elements within group?
<p>I want to split a list into n groups in all possible combinations (allowing for variable group length). </p>

<p>Say, I have the following list:</p>

<pre><code>lst=[1,2,3,4]
</code></pre>

<p>If I specify n=2, the list could be divided either into groups of 1 element-3 elements or 2 elements-2 elements. Within those two ways of splitting the list, there are unique combinations of which elements go in each list. </p>

<p>With n=2, these would be:</p>

<pre><code>(1),(2,3,4)
(2),(1,3,4)
(3),(1,2,4)
(4),(1,2,3)
(1,2),(3,4)
(1,3),(2,4)
(1,4),(2,3)
</code></pre>

<p>With n=1 these would be:</p>

<pre><code>(1,2,3,4)
</code></pre>

<p>And with n=3 these would be:</p>

<pre><code>(1),(2),(3,4)
(1),(3),(2,4)
(1),(4),(2,3)
(2),(3),(1,4)
(2),(4),(1,3)
(3),(4),(1,2)
</code></pre>

<p>I am not interested in groups of length 0, and order within a group does not matter.</p>

<p>I found two similar questions, but they don't answer my question exactly.</p>

<p><a href="https://stackoverflow.com/questions/5360220/how-to-split-a-list-into-pairs-in-all-possible-ways">This</a> question splits a list into all combinations where each group is of length n (I found the answer by @tokland) to be particularly useful). However, I am not looking for all groups to be of the same length. </p>

<p>And then the first step of <a href="https://stackoverflow.com/questions/9088321/show-all-possible-groupings-of-a-list-given-only-the-amount-of-sublists-length">this</a> question gets unique combinations of split locations to split a list into n groups. However, list order is preserved here and unique combinations of elements within these groups is not determined.</p>

<p>I am looking for a combination of these two questions - a list is split into n groups in all possible combinations of group length as well as combinations of elements within a group.</p>