Python Combinations
<p>Suppose I have a Python list that looks like:</p>

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

<p>I would like to be able to return a list of lists containing all combinations of two or more numbers. The order of the values doesn't matter so 1,2 is the same as 2,1.  I would also like to return another list that contains the values that do not feature in each combination. For example:</p>

<pre><code>Combination 1,2 / Remainder 3,4
Combination 2,3 / Remainder 1,4
Combination 1,2,3 / Remainder 4
Combination 1,2,3,4/ Remainder -
</code></pre>

<p>The returned lists for the above would be</p>

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

<p>I've only shown a few examples...</p>

<p>I realise that the first part can probably be achieved using <code>itertools.combinations</code> but how can I return those values not used in the combination without looping?</p>

<p>You can take the set difference:</p>

<pre><code>l = set([1, 2, 3, 4])

for i in range(len(l)+1):
    for comb in itertools.combinations(l, i):
        print(comb, l.difference(comb))

() {1, 2, 3, 4}
(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}
(2, 3) {1, 4}
(2, 4) {1, 3}
(3, 4) {1, 2}
(1, 2, 3) {4}
(1, 2, 4) {3}
(1, 3, 4) {2}
(2, 3, 4) {1}
(1, 2, 3, 4) set()
</code></pre>

python python-2.7 combinations itertools