15,610,088 members
See more:
The output of my code is as follows:

```1 4 6 8
1 4 6 9
1 4 7 9
1 5 7 9```

The desired output is as follows:

```1 4 6 8
1 4 6 9
1 4 7 8
1 4 7 9
1 5 6 8
1 5 6 9
1 5 7 8
1 5 7 9```

I want the loop to go through all possible combinations. Any help here is much appreciated.

What I have tried:

```var_a = ["1"]
var_b = ["4", "5"]
var_c = ["6", "7"]
var_d = ["8", "9"]

for x in var_a:
for y in var_b:
for z in var_c:
for a in var_d:
var_a=x
var_b=y
var_c=z
var_d=a
print(var_a,var_b,var_c,var_d)```
Posted
Updated 6-May-22 10:59am
v2

## Solution 2

Try this
Python
```var_a = ["1"]
var_b = ["4", "5"] # this variable
var_c = ["6", "7"]
var_d = ["8", "9"]

for x in var_a:
for y in var_b:
for z in var_c:
for a in var_d:
var_a=x
var_b=y # is destroyed  here
var_c=z
var_d=a
print(var_a,var_b,var_c,var_d)
print(x,y,z,a)```

v2

## Solution 1

You should use numeric loops rather than characters. You can then set range values for each one. When a variable reaches its limit you then start the next value to the left. The innermost loop will go from 8 to 9, the next from 6 to 7, etc.

## Solution 3

Why don't you break this into 3 functioning loops and then add them up. Honestly I didn't check the code but personally I preffer the method I just told.
It might take some time but the answer worth the time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 58 OriginalGriff 55 Graeme_Grant 35 Dave Kreskowiak 10 Anant Tiwari 10
 OriginalGriff 2,476 Richard MacCutchan 1,071 Graeme_Grant 695 Dave Kreskowiak 631 CHill60 285

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900