title: variable number of dependent nested loops
body: <p>Given two integers <code>n</code> and <code>d</code>, I would like to construct a list of all nonnegative tuples of length <code>d</code> that sum up to <code>n</code>, including all permutations. This is similar to the <a href="https://stackoverflow.com/questions/10035752/elegant-python-code-for-integer-partitioning">integer partitioning problem</a>, but the solution is much simpler. For example for 
  <code>d==3</code>:</p>
  
  
  
  <pre><code>[
      [n-i-j, j, i]
      for i in range(n+1)
      for j in range(n-i+1)
  ]
  </code></pre>
  
  <p>This can be extended to more dimensions quite easily, e.g., <code>d==5</code>:</p>
  
  
  
  <pre><code>[
      [n-i-j-k-l, l, k, j, i]
      for i in range(n+1)
      for j in range(n-i+1)
      for k in range(n-i-j+1)
      for l in range(n-i-j-l+1)
  ]
  </code></pre>
  
  <p>I would now like to make <code>d</code>, i.e., the number of nested loops, a variable, but I'm not sure how to nest the loops then.</p>
  
  <p>Any hints?</p>