-2

I want to know how to condense this code using loops. It is a repeated sequence of (i) variable definition (ii) while loop condition ...(iii) variable increment

list_main = []
x1 = 1
while x1<= n:
    x2 = x1
    while x2 <= n:
        x3 = x2
        while x3 <= n:
#continue the sequence: x(i) = x(i-1) ; while x(i) <= n:
 
            list_ = [x1,x2,...]
            list_main.append(list_)
            x3 = x3 + 1
        x2 = x2 + 1
    x1 = x1 + 1

I want something like this:

max_x = 10 #maximum number of 'x's
#code
list_ = [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]

Edit

If Number of 'x's is 2 and n is 5, then my desired output, i.e., list_main is

list_main = [[1,1],[1,2],[1,3],[1,4],[1,5],[2,2],[2,3],[2,4],[2,5],[3,3],[3,4],[3,5],[4,4],[4,5],[5,5]]

If the number of 'x's is 3 and n is 3, then my desired output is

list_main = [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
user961447
  • 128
  • 3
S Das
  • 107
  • 6

1 Answers1

1

As pointed out in this answer, your problem can be solved like this:

max_x = 10
n = 5
values = []
i = 1
while i <= n:
    list_values.append(i)
    i = i + 1
count = [max_x] * n
list_main = list(unique_combinations_from_value_counts(values,count,max_x))
user961447
  • 128
  • 3