I am creating a program to generate a list of numbers in lexicographical order and I need nested for loops based on the number of elements in the input. I'd like to dynamically allocate/create nested for loops based on the number of elements in the input.
def bounded_lists(upper_bounds):
res = []
for i in range(0,max(upper_bounds) + 1):
for j in range(0,max(upper_bounds) + 1):
for k in range(0,max(upper_bounds) + 1):
if i<=upper_bounds[0] and j<=upper_bounds[1] and k<=upper_bounds[2]:
res.append([i,j,k])
return res
This code gives me the right result and is straightforward if I know that the number of elements in the input is 3.