# Generating And / Or Conditions Automatically from a List

I have the following list:

l1 = {x y, s t, a b}

From this list, I am trying to essentially generate a new list that looks like this:

    l1 = {x==1 && y==1 && s == 0 && t == 0 && a == 0 && b == 0
|| x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0
|| x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1}


I.e., only one pair at a time can take on the value of 1 in each OR statement.

l1 = {x y, s t, a b};

res1 = {Or @@
Inner[Sequence @@ Thread[Equal[List @@ #2, #]] &, IdentityMatrix[Length @ l1], l1, And]}


{(x == 1 && y == 1 && s == 0 && t == 0 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1)}

Alternatively,

l2 = Flatten[l1 /. Times -> List]


{x, y, s, t, a, b}

res2 = {Or @@
Inner[Equal[#2, #] &, Riffle[#, #] & /@ IdentityMatrix[Length[l2]/2], l2,  And]}


{(x == 1 && y == 1 && s == 0 && t == 0 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 1 && t == 1 && a == 0 && b == 0) ||
(x == 0 && y == 0 && s == 0 && t == 0 && a == 1 && b == 1)}

• Hi @kglr . I tried this solution, and it works great. The only problem I am having is that it doesn't work exactly as expected because your l1 is slightly different then mine. In your l1, you have l1 = {x,y,s,t,a,b}, but I have l1 = {x y, s t, a b}, i.e., each element in the list contains a product. – Thomas Moore Oct 24 '19 at 19:21
• @ThomasMoore, you can use l0 = {x y, s t, a b}; l1 = Flatten[l0 /. Times -> List]. I will post an alternative approach using {x y, s t, a b} directly. – kglr Oct 24 '19 at 19:24
• Hi @kglr Thank you very much. It is much appreciated! – Thomas Moore Oct 24 '19 at 19:24