[issue5048] Extending itertools.combinations

Mark Dickinson report at bugs.python.org
Sun Jan 25 14:08:37 CET 2009


Mark Dickinson <dickinsm at gmail.com> added the comment:

> that dropping the length argument will make the function iterate over 
> *all* combinations,

Now *this* sounds more useful to me:  an itertool that generates *all* subsets of a list.  It's 
quite easy to do with existing itertools, though, either by looping over the second argument to 
combinations, or (for a different ordering) using product:

def subsets(iterable):
    l = list(iterable)
    for selector in itertools.product([False, True], repeat=len(l)):
        yield [element for indicator, element in zip(selector, l) if indicator]

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5048>
_______________________________________


More information about the Python-bugs-list mailing list