# How do I map variables to lists and then join the lists together?

Hey guys I have three lists:

a={{1, 2}, {1, 3}, {1, 2}, {1, 2}, {1, 2}}

b={{{2, 3}, {1, 3}}, {{2, 3}, {1, 3}}, {{2, 3}, {1, 3}}, {{2, 3}, {1,3}}, {{2, 3}, {1, 2, 3}}}

c={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0}}


What I'm trying to figure out is how do I do the following: I want to add a "T" into the front of every number in all the sublists for list "a". Then I want to do something similar with list "b" but this time I want to add an "A" into the front of every number in all the sublists giving me the following lists:

aa={{T1, T2}, {T1, T3}, {T1, T2}, {T1, T2}, {T1, T2}}

bb={{{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A2, A3}}}


Lastly what I want to do is I want to match certain elements in list "aa" to list "bb" and then insert those matched elements into list "c" giving me a new list "cc". For example, in the first sublist of list "aa" there's a {T1,T2} that corresponds to the first sublist of {{A2,A3},{A1,A3}}.....} in list "bb". What I want to do is then take T1 and pair it to the first sublist of the list given for "bb". So T1 would be distributed to A2 and A3giving me elements of {T1,A2},{T1,A3}. Then taking T2 I want to distribute it to A1 and A3 to give me elements {T2,A1}, {T2,A3}.

That would overall give me elements {{T1,A2},{T1,A3},{T2,A1},{T2,A3}}. I then want to take those elements and insert them into list "c". List "c" is just comprised of 0's which are just placeholders. So in the first sublist of list "c" we have {{0,0,0,0,0}....}. I want to insert the elements above into list "c" to give me a new list "cc"

cc={{{T1,A2},{T1,A3},{T2,A1},{T2,A3}}...}}


I'm trying to find a way to do this process for all the sublists in "aa" and "bb" and input them all into "c" to give me a new list "cc".

In lists "aa" and "bb" each sublist correspond to one another. So in list "aa", the first sublist of {T1,T2} relates to the first sublist in list "bb" of {{{A2, A3}, {A1, A3}}.... The second sublist correspond together, e.g. in "aa" the second sublist {T1, T3} corresponds to the second sublist of {...{{A2, A3}, {A1, A3}}...} in list "bb", So on and so forth.

aa = Map[Symbol["T" <> ToString[#]] &, a, {-1}]

{{T1, T2}, {T1, T3}, {T1, T2}, {T1, T2}, {T1, T2}}

bb = Map[Symbol["A" <> ToString[#]] &, b, {-1}]

{{{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A3}},
{{A2, A3}, {A1, A3}}, {{A2, A3}, {A1, A2, A3}}}

ClearAll[combine]

cc  = combine[aa, bb]

{{{T1, A2}, {T1, A3}, {T2, A1}, {T2, A3}},
{{T1, A2}, {T1, A3}, {T3, A1}, {T3, A3}},
{{T1, A2}, {T1, A3}, {T2, A1}, {T2, A3}},
{{T1, A2}, {T1, A3}, {T2, A1}, {T2, A3}},
{{T1, A2}, {T1, A3}, {T2, A1}, {T2, A2}, {T2, A3}}}


Alternatively:

ClearAll[combine2, combine3]

combine3 = Map[Apply[Join] @* Map[Thread]] @* Map[Transpose] @* Transpose @* List;

cc == combine2[{aa, bb}] ==combine3[aa, bb]

True


Notes:

To get some intuition on how combine works, consider first an arbitrary function foo in the first argument of MapThread:

MapThread[foo, {aa, bb}] // Column If we try Thread[{##}]& in place of foo:

MapThread[Thread[{##}] &, {aa, bb}] // Column Squinting at each sublist suggest one more round of Thread will get us closer:

MapThread[Thread /@ Thread[{##}] &, {aa, bb}] // Column We are almost there. Need to get rid unwanted braces which is what Join does:

MapThread[Join @@ (Thread /@ Thread[{##}]) &, {aa, bb}] // Column You might enjoy the flash animations in the following link of all the functions used above:

MapThread: Thread: Map: Apply: Join: • Hey could you please elaborate on what's exactly happening during the "combine" section? I'm a little unfamiliar with some of the syntax used in Mathematica – D'Angelo Apr 20 '20 at 20:38
• @D'Angelo, hope the notes i added help. – kglr Apr 20 '20 at 21:46
• Awesome, that was amazingly insightful, thank you. – D'Angelo Apr 20 '20 at 21:51