title: Python : DIY generalize this "all_subsets" function to any size subsets
body: <p>Implementing a toy <a href="http://en.wikipedia.org/wiki/Apriori_algorithm" rel="nofollow">Apriori algorithm</a> for a <em>small</em>-data association rule mine, I have a need for a function to return all subsets.</p>
  
  <p>The length of the <a href="http://en.wikipedia.org/wiki/Subset" rel="nofollow">subsets</a> is given by parameter <code>i</code>. <strong>I need to generalize this function for any <code>i</code>.</strong> The cases for <code>i</code> 1 or 2 are trivial, and the general pattern can be seen : a list of tuples of length <code>i</code> where order is imposed to prevent duplicates. </p>
  
  <pre><code>def all_subsets(di,i):
          if i == 1:
                  return di
          elif i == 2:
                  return [(d1,d2) for d1 in di for d2 in di if d1 &lt; d2]
          else:
                  return [ ... ]
  </code></pre>
  
  <p>How can I generalize this <code>i</code> nested loops pattern in a concise manner, say using list comprehensions, generators or some "functional programming" concepts? </p>
  
  <p>I was thinking of some kind of list of functions, but I don't really know how I can generalize <code>i</code> nested loops. Any hints or full answers will be treated as awesome.</p>