# Making a List of Lists with equal structure of another List of Lists

I am trying to do something like this:

If l1, l2, p1, p2, q1, q2 and q3 are lists then I want to know how I can obtain the following list:

in1 = {l1, l2}
in2 = {{p1,p2},{q1,q2,q3}}
output = {{l1,p1},{l1,p2},{l2,q1},{l2,q2},{l2,q3}}


I am doing it with the following code:

step1[l1_, l2_] := Table[l1, {Length@l2}]

step2[l1_, l2_] := step1 @@@ (Transpose@{l1, l2})

final[l1_, l2_] :=  Transpose@{Flatten[#, 1] &@step2[l1, l2], Flatten[#, 1] &@l2}


But I want to know if there are other options.

in1 = {l1, l2};
in2 = {{p1, p2}, {q1, q2, q3}};

Inner[List, in1, in2, Join @@ Thread /@ {##} &]
Inner[Thread @ {##} &, in1, in2, Join]
Flatten[Tuples /@ Transpose[{List /@ in1, in2}], 1]
Flatten[MapThread[Tuples[{{#1}, #2}] &, {in1, in2}], 1]
Flatten[MapThread[Thread[{##}] &, {in1, in2}], 1] (* thanks:  aardvark2012 *)
MapThread[Outer[List, {#1}, #2] &, {in1, in2}]~Flatten~2 (* thanks:  Jason B. *)


all give

{{l1, p1}, {l1, p2}, {l2, q1}, {l2, q2}, {l2, q3}}

• similar: MapThread[Outer[List, {#1}, #2] &, {in1, in2}]~Flatten~2 Commented Sep 12, 2017 at 20:28
• @JasonB., nice; i added a variant with Tuples.
– kglr
Commented Sep 12, 2017 at 20:43
• Also Flatten[MapThread[Thread[{#1, #2}] &, {in1, in2}], 1] Commented Sep 12, 2017 at 22:26
• @aardvark2012, thank you. I added your suggestion.
– kglr
Commented Sep 12, 2017 at 23:30
• Maybe I misunderstood the question but if l2, p1, q3 etc. have arbitrary lists assigned to them only a couple of these methods work. Commented Sep 13, 2017 at 0:40