Variables value’s combinations with constrains in Python

The following short script shows how Python is powerful, in this case helping us with combinatorial calculus. This script calculates and prints all combinations of variables values A,B,C,… , also considering constrains among variables that frequently happen to deal with in combinatorial testing problems.

# Data array
A = ("a1", "a2", "a3", "a4", "a5");
B = ("b1", "b2", "b3", "b4", "b5");
C = ("c1", "c2", "c3", "c4", "c5");
D = ("d1", "d2", "d3", "d4", "d5");

# Combinations array
cmb = [
    a + " " + b + " " + c + " " + d
    for a in A
    for b in B
    for c in C
    for d in D
    # Constrains
    if
    ( 
       (a=="a1" or a=="a2") and (c != "c1")   or  # Equivalent to condition:
       (a != "a1" and a != "a2")                  # IF (a=="a1" or a=="a2") THEN (c != "c1") 
    )
    
];

i=0;
for item in cmb:
    i += 1;
    print (str(i) + ":" + item);

Just to explain a bit: using list (or better... tuples) comprehension, the tupla called cmb hold the Cartesian product (the code: a + " " + b + " " + c + " " + d) of the A, B, C and D variables values (tuples A, B, C and D defined on the top of the script), considering the constrain (just an example):

if (a=="a1" or a=="a2") and (c != "c1")   or (a != "a1" and a != "a2")

that is equivalent to:

IF (a=="a1" or a=="a2") THEN (c != "c1") .

The output consist in 575 rows (not all reported here):

1:a1 b1 c2 d1
2:a1 b1 c2 d2
3:a1 b1 c2 d3
4:a1 b1 c2 d4
5:a1 b1 c2 d5

.................

572:a5 b5 c5 d2
573:a5 b5 c5 d3
574:a5 b5 c5 d4
575:a5 b5 c5 d5

Just take a look to the equivalent (more or less...) code in C++ (without constrains) to perform the same algorithm - also considering that C++ code should be improved and optimized -  you can "perceive" the difference!

The C++ program take input variable values reading an external file. But, in Python, you have just to update the script with correct values and constrains (as plain text) and run it; so the benefit to have an external file... is not so an advantage.