Python - How to sum all combinations of a list of numbers in order to reach a goal. Use of number is optional
<p>I have a list of numbers</p>

<pre><code>lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]
</code></pre>

<p>that I would like to sum in various ways in order to try and reach the goal number of 8276. I also need to see whether throwing away the cents or rounding helps reaching the goal. Note that use of a number is optional.</p>

<p>I tried </p>

<pre><code>from itertools import combinations
lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]
for i in xrange(1, len(lis) + 1):   #xrange will return the values 1,2,3,4 in this loop
    if sum(list(combinations(lis, i))) == 8276:
       print list(combinations(lis, i))
</code></pre>

<p>but this gives me </p>

<pre><code>TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
</code></pre>

<p>which I'm not sure why or how to fix.</p>

<p>You're trying to sum all the combinations with given length instead of calculating a sum of a single combination. Instead you should loop over the combinations and check the sum for each one:</p>

<pre><code>from itertools import combinations
lis = [497.96, 10, 5084, 156.43, 381.3, 3298.85, 625.68]
for i in xrange(1, len(lis) + 1):
    for comb in combinations(lis, i):
       if sum(comb) == 8276:
           print comb
</code></pre>

<p>The reason for the specific error is that <a href="https://docs.python.org/2.7/library/functions.html#sum" rel="nofollow"><code>sum</code></a> takes optional argument <code>start</code> which is the default value. If argument is not provided it defaults to <code>0</code>. Essentially your original code is trying to do following:</p>

<pre><code>&gt;&gt;&gt; sum([(1,), (2,)])
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
</code></pre>

python sum combinations permutation