title: Getting all addition and subtraction combinations of a list python
body: <p>I am trying to find all of the addition and subtraction combinations of a list of numbers. I want to create a recursive function that does this, but I am having trouble conceptualizing how this would work. This is my code right now</p>
  
  <pre><code>from itertools import permutations
  values = [1, 2]
  perm = []
  
  size = 1
  while size &lt;= len(values):
      perm += permutations(values, size)
      size = size + 1
  
  for i in perm:
      someFuntion(i)
  </code></pre>
  
  <p>I want the output to be this for the given list.</p>
  
  <pre><code>(1)
  (2)
  (1 + 2)
  (1 - 2)
  (2 + 1)
  (2 - 1)
  </code></pre>
  
  <p>Can someone help me conceptualize how I would write "someFunction"?</p>
  
  <p>Edit: So I updated my function and it works for lists of size 2, but I'm having trouble with larger lists. Can you help explain how you return a list without passing one into the function?</p>
  
  <pre><code>def some_function(i, results):
  
      # Base case
      if len(i) == 0:
          return 
      # Base case
      elif len(i) == 1:
  
          if i[0] not in results:
              results.append(i[0])
  
          return i[0]
  
      else:
  
          topVal = str(i.pop(0))
  
          plus = topVal + " + " + str(some_function(i, results))
          minus = topVal + " - " + str(some_function(i, results))
          results.append(plus)
          results.append(minus)
  
  
  results = []
  possibilities = list(flatten(map(lambda s: list(permutations(s)), powerset(values))))
  
  for p in possibilities:
      some_function(list(p), results)
  
  print(results)
  </code></pre>
  
  <p>This is my output for a list = [1, 2, 3]</p>
  
  <pre><code>['1', '2', '3', '1 + 2', '1 - 2', '2 + 1', '2 - 1', '1 + 3', '1 - 3', '3 + 1', '3 - 1', '2 + 3', '2 - 3', '3 + 2', '3 - 2', '2 + 3', '2 - 3', '1 + None', '1 - 3', '3 + 2', '3 - 2', '1 + None', '1 - 2', '1 + 3', '1 - 3', '2 + None', '2 - 3', '3 + 1', '3 - 1', '2 + None', '2 - 1', '1 + 2', '1 - 2', '3 + None', '3 - 2', '2 + 1', '2 - 1', '3 + None', '3 - 1']
  </code></pre>
  
  <p>Edit 2: I figured it out based on your second edit. Thanks for taking the time to walk me through this. I think this will help me write recursive functions better in the future.</p>