Skip to content

Home

Powerset

Returns the powerset of a given iterable.

from itertools import chain, combinations

def powerset(iterable):
  s = list(iterable)
  return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))

powerset([1, 2]) # [(), (1,), (2,), (1, 2)]

More like this

Start typing a keyphrase to see matching snippets.