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>Your problem is basically one of find all subsets of a given set (multiset in your case). Once you have the subsets its straight forward to construct the output you've asked for.</p>

<p>For a set A find all the subsets [S0, S1, ..., Si]. For each subset Si, take <code>(A - Si) | product(Si)</code>, where <code>|</code> is union and <code>-</code> is a set difference. You might not be interested in subsets of size 0 and 1, so you can just exclude those.</p>

<p>Finding subsets is a well known problem so I'm sure you can find resources on how to do that. Keep in mind that there are 2**N setbsets of a set with N elements.</p>

python algorithm combinations combinatorics