This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ataher
Recipients ataher, docs@python
Date 2020-06-25.23:11:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1593126682.32.0.703433792229.issue41120@roundup.psfhosted.org>
In-reply-to
Content
In the documentation the following example is given:

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

The proposed enhancement uses a nested generator so no intermediate results are created.

def product2(*args, repeat=1):
    def concat(result, pool):
        yield from (x+[y] for x in result for y in pool)
        
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = concat(result, pool)
    for prod in result:
        yield (tuple(prod))
History
Date User Action Args
2020-06-25 23:11:22atahersetrecipients: + ataher, docs@python
2020-06-25 23:11:22atahersetmessageid: <1593126682.32.0.703433792229.issue41120@roundup.psfhosted.org>
2020-06-25 23:11:22ataherlinkissue41120 messages
2020-06-25 23:11:22atahercreate