# Transpose of two lists with different length

I solve the equation of form $$f(x,y,z)=0$$ numerically for a given list {x,y} and put roots z0 into array. For several values $$x$$ & $$y$$ there is more then one root of equation.

Then I would like to organize the nested list with structure { {x,y}, z } where z can be a list. For instance,

   {   {x1,y1},{{z1,z2,z3}}  }


There is no problem to do it,

Transpose[{xyarray,zroots}]


gives me desirable result. But then I stuck with several problems:

1. How can I obtain the list of roots with structure

{   {{x1,y1},z1}, {{x1,y1},z2}, {{x1,y1},z3}, ...}


I can do it with many manipulations with Transpose but I believe that there is more simple way

2. Suppose that I have not only list of roots but list of pairs {z1, g[z1]} where $$g$$ is simple and known function. I compute list g[zarray] and then organize list of pairs. Finally, I work with list with structure

{ {{x1,y1},{ {z1,g[z1]}}, {z2,g[z2}}, {z3,g[z3]} },... }

In this list I would like to do several things. First, extract all the elements with $$g(z_i)<0$$. I understand how to do this with Cases, no problem. Then, I would like to obtain list with structure

{   {{x1,y1},{z1,g[z1]}},   {{x1,y1},{z2,g[z2]}, ...}


Therefore, finally I look for the following manipulation with lists:

{ {{x,y},list} }----->{  {{x,y},list[]}, {{x,y},list[]},... }


Here are two alternatives that seem to produce the format you want:

original = {{{1, 2}, {z1, z2, z3}}, {{5, 5}, {z7, z8, z9}}};
Flatten[Distribute[{{#1}, #2}, List] & @@@ original, 1]
Flatten[Outer[List, {#1}, #2, 1] & @@@ original, 2]

(* Out:
{
{{1, 2}, z1}, {{1, 2}, z2}, {{1, 2}, z3},
{{5, 5}, z7}, {{5, 5}, z8}, {{5, 5}, z9}
} *)

• Thank you very much! Jun 11 '20 at 16:51
• This does indeed work with the pairs that @ArtemAlexandrov explicitly indicates they want to modify. I think it might be helpful if you clarify this point, you can use the lister I defined in my answer if you’d like. Good show! Jun 11 '20 at 17:22
list1 = {{{1, 2}, {z1, z2, z3}}, {{5, 5}, {z7, z8, z9}}};

list2 = MapAt[{#, g @ #} &, list1, {All, 2, All}]

{{{1, 2}, {{z1, g[z1]}, {z2, g[z2]}, {z3, g[z3]}}},
{{5, 5}, {{z7, g[z7]}, {z8, g[z8]}, {z9, g[z9]}}}}


ClearAll[f1, f2, f3]
f1 = Join @@ (Tuples[{{#}, #2}] & @@@ #) &;
f2 = Join @@ Map[Thread[#, List, {2}] &]@# &;
f3 = Join @@ (Transpose[{ConstantArray[#, Length@#2], #2}] & @@@ #) &;

f1 @ list1

{{{1, 2}, z1}, {{1, 2}, z2}, {{1, 2}, z3},
{{5, 5}, z7}, {{5, 5},  z8}, {{5, 5}, z9}}

f1 @list2

{{{1, 2}, {z1, g[z1]}}, {{1, 2}, {z2, g[z2]}}, {{1, 2}, {z3, g[z3]}},
{{5, 5}, {z7, g[z7]}}, {{5, 5}, {z8, g[z8]}}, {{5, 5}, {z9, g[z9]}}}

f1 @ # == f2 @ # == f3 @ # & @ list1

 True

f1 @ # == f2 @ # == f3 @ # & @ list2

 True


Say you’ve got you list as such:

list=Array[{#1,#2}&,{1,10}][]

(* {{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10}} *)


Then you can do like so

{{x,y},list[[#]]}&/@Range@Length@list

(* {{{x,y},{1,1}},{{x,y},{1,2}},{{x,y},{1,3}},{{x,y},{1,4}},{{x,y},{1,5}},{{x,y},{1,6}},{{x,y},{1,7}},{{x,y},{1,8}},{{x,y},{1,9}},{{x,y},{1,10}}} *)


This is my go to for a lot of things I do. It can likely be done in a better way but hey! It works.

If you want to do the equivalent process on a set of these, you can do the following:

Cases[{a__List,b__List}:>({a,b[[#]]}&/@Range@Length@b)][original]//Flatten[#,1]&


Using original as defined by MarcoB, it produces the same output they show.

This same syntax can turn this

lister={{{x1,y1},{{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10}}},{{x2,y2},{{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10}}}};


Into this

Cases[{a__List,b__List}:>({a,b[[#]]}&/@Range@Length@b)][lister]//Flatten[#,1]&

{{{x1,y1},{1,1}},{{x1,y1},{1,2}},{{x1,y1},{1,3}},{{x1,y1},{1,4}},{{x1,y1},{1,5}},{{x1,y1},{1,6}},{{x1,y1},{1,7}},{{x1,y1},{1,8}},{{x1,y1},{1,9}},{{x1,y1},{1,10}},{{x2,y2},{1,1}},{{x2,y2},{1,2}},{{x2,y2},{1,3}},{{x2,y2},{1,4}},{{x2,y2},{1,5}},{{x2,y2},{1,6}},{{x2,y2},{1,7}},{{x2,y2},{1,8}},{{x2,y2},{1,9}},{{x2,y2},{1,10}}}

• Is this the format requested though? I understood them to start out with e.g. { {{1, 1}, {1, 2, 3}}, {{5, 5}, {7, 8, 9}} } and wanting { {{1, 1}, 1}, {{1, 1}, 2}, {{1, 1}, 3}, {{5, 5}, 7}, {{5, 5}, 8}, {{5, 5}, 9}, etc etc } Jun 11 '20 at 15:31
• Unfortunately, it do not seem what I need and @MarcoB is right Jun 11 '20 at 15:42
• @MarcoB that makes sense. I was only going off of the last point/request that ArtemAlexandrov stressed. I’ll come back to this a bit later & submit something regardless. Jun 11 '20 at 15:47
• @ArtemAlexandrov I am confused then what your ultimate end goal is. I show how to do essentially what you have iterated at the end of your question as what you want to happen. This is why I show it with a list of pairs as you indicate. It would be helpful to clarify this point. Please help me to understand what it is you want to achieve. Jun 11 '20 at 17:11
• @CATrevillian I try to clarify as I can. The answer by MarcoB is exactly what I look for Jun 11 '20 at 17:15