I'm trying to create a powerset in Python 3. I found a reference to the itertools
module, and I've used the powerset code provided on that page. The problem: the code returns a reference to an itertools.chain
object, whereas I want access to the elements in the powerset. My question: how to accomplish this?
Many thanks in advance for your insights.
itertools
functions return iterators, objects that produce results lazily, on demand.You could either loop over the object with a
for
loop, or turn the result into a list by callinglist()
on it:You can also store the object in a variable and use the
next()
function to get results from the iterator one by one.Here's a solution using a generator: