Into itertools

AggieDan04 danb_83 at
Sun Apr 26 19:19:11 CEST 2009

On Apr 26, 11:32 am, bearophileH... at wrote:
> Some idioms are so common that I think they deserve to be written in C
> into the itertools module.
> 1) leniter(iterator)
> 2) xpairwise(iterable)
> 3) xpairs(seq)
> 4) xsubsets(seq)

Good suggestions.  Another useful function I'd like to see in
itertools is the Cartesian product.  This can be implemented as:

def cartesian_product(*args):
    """Iterates over the Cartesian product of args[0], args[1], ..."""
    if not args:
    elif len(args) == 1:
        for item in args[0]:
            yield (item,)
        for item in args[0]:
            for item2 in cartesian_product(*args[1:]):
                yield (item,) + item2

More information about the Python-list mailing list