Improved itertools recipe for generating powerset().
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 903284e..b7cd431 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -687,11 +687,9 @@
nexts = cycle(islice(nexts, pending))
def powerset(iterable):
- "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])"
- # Recipe credited to Eric Raymond
- pairs = [(2**i, x) for i, x in enumerate(iterable)]
- for n in xrange(2**len(pairs)):
- yield set(x for m, x in pairs if m&n)
+ "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
+ s = list(iterable)
+ return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def combinations_with_replacement(iterable, r):
"combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"