Variable forloop for possibilities generatization

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.

                                                                                                   ByDebbitx.

You don’t mention why you want these loops, but often they can be replaced with the product generator from itertools.

from itertools import product

atx=[0,1,2,3,4]
for looo, loo, lo in product(atx, repeat=3):
    print(looo, loo, lo)

Thank you Mr BowlOfRed

i wanted to say,i dont know number of looo,loo,lo …n maybe be looo ,loo,lo,l,l1,l2 or loo,lo i dont know ,
this number of variables can change

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))
('R', 'R')
('R', 'G')
('R', 'B')
('G', 'R')
('G', 'G')
('R', 'R', 'R', 'R')
('R', 'R', 'R', 'G')
('R', 'R', 'R', 'B')
('R', 'R', 'G', 'R')
('R', 'R', 'G', 'G')
1 Like

Than Yuou So much My bro.