Determine list of all possible products from a list of integers in Python
<p>In Python 2.7 I need a method that returns all possible products of a <code>list or tuple of int</code>. Ie. if input is <code>(2, 2, 3, 4)</code>, then I'd want a output like</p>

<ul>
<li><code>(3, 4, 4)</code>, 2 * 2 = 4</li>
<li><code>(2, 4, 6)</code>, 2 * 3 = 6</li>
<li><code>(2, 3, 8)</code>, 2 * 4 = 8</li>
<li><code>(3, 4, 4)</code>, 2 * 2 = 4</li>
<li><code>(2, 2, 12)</code>, 3 * 4 = 12</li>
<li><code>(2, 24)</code>, 2 * 3 * 4 = 24</li>
<li><code>(3, 16)</code>, 2 * 2 * 4 = 16</li>
<li><code>(4, 12)</code>, 2 * 2 * 3 = 12</li>
<li><code>(48)</code>, 2 * 2 * 3 * 4 = 48</li>
</ul>

<p>wrapped up in a <code>list or tuple</code>. I figure that a nice implementation is probably possible using <a href="https://docs.python.org/2/library/itertools.html#itertools.combinations" rel="nofollow">combinations</a> from <code>itertools</code>, but I'd appreciate any help. Note that I am only interested in distinct lists, where order of <code>int</code> plays no role.</p>

<p>EDIT</p>

<p>Some futher explanation for some clarification. Take the first output list. Input is <code>(2, 2, 3, 4)</code> (always). Then I take 2 and 2 <em>out</em> of the list and multiply them, so now I am left with a list <code>(3, 4, 4)</code>. 3 and 4 from the input and the last 4 from the product. </p>

<p>I haven't tried anything yet since I just can't spin my head around that kind of loop. But I can't stop thinking about the problem, so I'll add some code if I do get a suggestion.</p>

<p>Suppose you have a vector of 4 numbers (for instance (2,2,3,4)).</p>

<p>You can generate a grid (as that one showed below):</p>

<pre><code>0 0 0 0
0 0 0 1
0 0 1 0 
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
</code></pre>

<p>Now remove the rows with all '0' and the rows with only one '1'.</p>

<pre><code>0 0 1 1
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
</code></pre>

<p>Now you can substitute the '1' with the respective element in the vector.
If your vector is (2,2,3,4) it becomes:</p>

<pre><code>0 0 3 4
0 2 0 4
0 2 3 0
0 2 3 4
2 0 0 4
2 0 3 0
2 0 3 4
2 2 0 0
2 2 0 4
2 2 3 0
2 2 3 4
</code></pre>

<p>Try to implement this in Python.
Below a pseudo code:</p>

<pre><code>for i from 0 to 2^VECTOR_LEN:
    bin=convert_to_binary(i)
    if sum_binary_digit(bin) &gt; 1:
       print(exec_moltiplication(bin,vector) 
       # if you want you can also use the bin vector as mask for the updating 
       # of your tuple of int with the result of the product and append it
       # in a list (as in your example).
       # For example if bin is (1 1 0 0) you can edit (2 2 3 4) in (4 3 4)
       # and append (4 3 4) inside the list or if it is (1 0 1 0) you can 
       # update (2 2 3 4) in (6 2 4)
</code></pre>

<p>WHERE:</p>

<ul>
<li>vector: is the vector containing the numbers</li>
<li>VECTOR_LEN is the length of vector</li>
<li>convert_to_binary(num) is a function that convert an integer (num) to binary</li>
<li>sum_binary_digit(bin) is a function that sum the 1s in your binary number (bin)</li>
<li>exec_multiplication(vector,bin) take in input the vector (vector) and the binary (bin) and returns the value of the multiplication.</li>
</ul>

python algorithm combinations combinatorics