itertools.product
is a generator, and you can easily chain generators together. Here's a generator expression that changes each tuple yielded by product
into a list:
(list(tup) for tup in itertools.product(iterable1, iterable2, etc))
In your example code, you could use the generator expression, or you could use a different approach to add an extra value on to the front of your values while keeping them as tuples:
for r0 in itertools.product([0, 1], repeat=3):
r0 = (0,) + r0 # keep r0 a tuple!
for r1 in itertools.product([0, 1], repeat=3):
r1 = (1,) + r1 # same here
# ...
Since you don't show what you're using your rN
variables for, it's impossible to give you a definitive answer as to what would be the best way to go. (It is a bit of a code smell that you have numbered variables.) Indeed, since your loops are simply computing three more 0
or 1
digits, you might be able to get away with a single product
call, that generates an list of n
different r
values in one go:
for bits in itertools.product([0, 1], repeat=3*n):
rs = [(i,) + bits[3*i:3*i+3] for i in range(n)]
# do something with the list of r tuples here