i want to code variable forloops up to the number of digits
_ _ _ three digits
_ _ _ _ four digits
_ _ _ _ _ five digits
example
atx=[0,1,2,3,4]
i know three digits in loop i coded the as follows
for looo in atx:
for loo in atx:
for lo in atx:
print(loo,loo,lo)
But examle when i dont konow digit number how can i code the structure of variable number of for loops
Thank you so much.
It seems to me you need to know how many loops there should be when you call it. But even so, you can just change the repeat value. And you don’t have to expand it immediately. You can get a tuple of values and it will have as many elements as you ask it for.
from itertools import product
atx=["R", "G", "B"]
# print first 5 choices with 2 "digits"
p = product(atx, repeat=2)
for _ in range(5):
print(next(p))
# print first 5 choices with 4 "digits"
p = product(atx, repeat=4)
for _ in range(5):
print(next(p))