1
for a in range(min_num,max_num+1):
    for b in range(min_num,max_num+1):
        for c in range(min_num,max_num+1):
            for d in range(min_num,max_num+1):
                for e in range(min_num,max_num+1):
                    rows=[e,d,c,b,a]

how would I go about writing this such that rows can be any length? I would like to do this without importing any modules

martineau
  • 119,623
  • 25
  • 170
  • 301
God Of Jam
  • 33
  • 5
  • 4
    event built in modules like itertools? – Christian Sloper Oct 13 '20 at 16:16
  • 1
    The requirement to do it without itertools is odd, but you can always put the [same logic as `itertools.product`](https://github.com/python/cpython/blob/master/Modules/itertoolsmodule.c#L2080) into your program. – ggorlen Oct 13 '20 at 16:22

2 Answers2

0
from itertools import product

for i in product(range(3),repeat=3):
    print(i)

gives you

(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(0, 2, 0)
(0, 2, 1)
(0, 2, 2)
(1, 0, 0)
(1, 0, 1)
(1, 0, 2)
(1, 1, 0)
(1, 1, 1)
(1, 1, 2)
(1, 2, 0)
(1, 2, 1)
(1, 2, 2)
(2, 0, 0)
(2, 0, 1)
(2, 0, 2)
(2, 1, 0)
(2, 1, 1)
(2, 1, 2)
(2, 2, 0)
(2, 2, 1)
(2, 2, 2)
Lukas S
  • 3,212
  • 2
  • 13
  • 25
0

A generator solution:

xss = [],
for _ in range(n):
    xss = ([x] + xs for xs in xss for x in range(min_num, max_num+1))

Demo of using the result (with n = 5 and min_num, max_num = 7, 9):

>>> for xs in xss:
        print(xs)

[7, 7, 7, 7, 7]
[8, 7, 7, 7, 7]
[9, 7, 7, 7, 7]
[7, 8, 7, 7, 7]
[8, 8, 7, 7, 7]
[9, 8, 7, 7, 7]
[7, 9, 7, 7, 7]
[8, 9, 7, 7, 7]
[9, 9, 7, 7, 7]
[7, 7, 8, 7, 7]
...
[9, 9, 8, 9, 9]
[7, 7, 9, 9, 9]
[8, 7, 9, 9, 9]
[9, 7, 9, 9, 9]
[7, 8, 9, 9, 9]
[8, 8, 9, 9, 9]
[9, 8, 9, 9, 9]
[7, 9, 9, 9, 9]
[8, 9, 9, 9, 9]
[9, 9, 9, 9, 9]
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65