Using itertools.product in place of double-nested for loop in Python 3

Question:

The following code works, but appears verbose.

def gen(l):
    for x in range(l[0]):
        for y in range(l[1]):
            for z in range(l[2]):
                yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))

>>>[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2]]

My intention was to cut down on LOC with itertools.product. Here’s what I came up with.

from itertools import product
def gen(l):
    for x, y, z in product(map(range, l)):
        yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))

ValueError: not enough values to unpack (expected 3, got 1)

Is there a different way to use itertools.product so that there are enough values to unpack?

Asked By: CannedSpinach

||

Answers:

You need to pass the elements of the map iterator to product separately with *:

for x, y, z in product(*map(range, l))

Incidentally, with another map call, you could save another line, skip the overhead of a Python generator, and do all the work in C:

def gen(l):
    return map(list, product(*map(range, l)))
Answered By: user2357112