# Combinations of a variable number of lists with different number of elements

Say we have $$n$$ lists with a variable number of elements that we need to pick one element from each one, and combining them to produce one resultant list with every element of the input lists. The input lists have a familiar pattern as described below. They are not random.

Say we have 2 lists like:

l1={0,1,2}   (* the first and the last list is always this one. *)
l2={0,1,2}   (* the last list is always equal to the first and with these elements *)


and the result we expect is

l3={{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}} with 9 elements.


If we have 3 lists they will be:

l1={0,1,2}           (* the first and the last are always this one *)
l2={0,1,2,3}         (* the second have one more element than the first *)
l3={0,1,2}           (* the last one is always equal to the first *)


and the expected result would be:

l4={{0,0,0},{1,0,0},{2,0.0}.........{0,3,2},{1,3,2},{2,3,2}} (total of 36 elements.


If we have 4 lists they will be:

l1={0,1,2}            (* As always this is the first *)
l2={0,1,2,3}          (* The second has one digit more *)
l3={0,1,2,3}          (* The third is equal to the second because n is even *)
l4={0,1,2}            (* The last is always equal to the first *)


and the expected result will be like:

l5={{0,0,0,0},.........{2,3,3,2}} with 144 elements


If we have 5 lists it will be:

l1={0,1,2}            (* As always the first *)
l2={0,1,2,3}          (* The second one digit more *)
l3={0,1,2,3,4}        (* This one has one more digit because n is odd *)
l4={0,1,2,3}          (* This one is always equal to the second *)
l5={0,1,2}            (* This one is always equal to the first *)


and the expected result:

l6={{0,0,0,0,0}........{2,3,4,3,2}} with 720 elements


With 6 lists it will be like:

l1={0,1,2}         (* The first is always this one *)
l2={0,1,2,3}       (* The second has one digit more *)
l3={0,1,2,3,4}     (* The third has one digit more *)
l4={0,1,2,3,4}     (* The fourth repeats because n is even *)
l5={0,1,2,3}       (* The next to last is always equal to the second *)
l6={0,1,2}         (* The last is always this one *)


and the expected resulting list:

l7={{0,0,0,0,0,0}.......{2,3,4,4,3,2}} with 3600 elements


The input to the program will just be $$n$$ since the lists will be generate with the above pattern (like a Pascal triangle).

Please feel free to get in touch if more clarification is needed.

just select n and all lists will be produced plus the final list

n = 3;
t = n/2;
f = If[EvenQ@n, t = t - 1;
Join[s = Table[Range[0, 2 + i], {i, 0, t}], Reverse@s],
Join[s = Table[Range[0, 2 + i], {i, 0, t}], Reverse@s[[;; -2]]]] ;
Column@f

T=Tuples[f]
Length@T


{0,1,2}
{0,1,2,3}
{0,1,2}

{{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 1, 0}, {0, 1, 1}, {0, 1, 2}, {0, 2, 0}, {0, 2, 1}, {0, 2, 2}, {0, 3, 0}, {0, 3, 1}, {0, 3, 2}, {1, 0, 0}, {1, 0, 1}, {1, 0, 2}, {1, 1, 0}, {1, 1, 1}, {1, 1, 2}, {1, 2, 0}, {1, 2, 1}, {1, 2, 2}, {1, 3, 0}, {1, 3, 1}, {1, 3, 2}, {2, 0, 0}, {2, 0, 1}, {2, 0, 2}, {2, 1, 0}, {2, 1, 1}, {2, 1, 2}, {2, 2, 0}, {2, 2, 1}, {2, 2, 2}, {2, 3, 0}, {2, 3, 1}, {2, 3, 2}}

36

• Oh that was fast. It worked perfectly. Thanks! – Giorgio Nov 1 '18 at 23:16
• I'm glad I helped! – J42161217 Nov 1 '18 at 23:24
Distribute[{l1, l2}, List]


{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}

Distribute[{l1, l2, l3}, List] // Length


36

Distribute[{l1, l2, l3, l4, l5, l6}, List] // Length


3600

In addition, Outer may be used:

(Outer[List, l1, l2, l3, l4, l5, l6] // Flatten[#, 5] &) ==
Distribute[{l1, l2, l3, l4, l5, l6}, List]


True

• Wow, even shorter. Thanks! Trying right now. – Giorgio Nov 2 '18 at 1:46

Update:

f = With[{a = Range[Ceiling[#/2]], b = - 1 - Mod[#, 2]},
Tuples @ Range[0, 1 + Join[a, Reverse[a[[;; b]]]]]] &;

f


{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}

Length[f @ #] & /@ Range[2, 10]


{9, 36, 144, 720, 3600, 21600, 129600, 907200, 6350400}

Tuples[{l1, l2}]

Length@Tuples[{l1, l2, l3, l4, l5, l6}]