title: What is the fastest way of computing powerset of an array in Python?
body: <p>Given a list of numbers, e.g. <code>x = [1,2,3,4,5]</code> I need to compute its powerset (set of all subsets of that list). Right now, I am using the following code to compute the powerset, however when I have a large array of such lists (e.g. 40K of such arrays), it is extremely slow. So I am wondering if there can be any way to speed this up.</p>
  
  <pre><code>superset = [sorted(x[:i]+x[i+s:]) for i in range(len(x)) for s in range(len(x))]
  </code></pre>
  
  <p>I also tried the following code, however it is much slower than the code above.</p>
  
  <pre><code>from itertools import chain, combinations
  
  def powerset(x):
      xx = list(x)
      return chain.from_iterable(combinations(xx, i) for i in range(len(xx)+1))
  </code></pre>