# Transpose two uneven lists [duplicate]

I am trying to plot two lists using the ListPlot command. I need to merge two lists.

The two lists I have are:

a = {1, 2, 3, 4, 5, 6, ....}
b = { {1}, {0, 0}, {2, 248, 234, 5, 6}, ...}

I need each element of list "a" to map to list 'b's individual lists. For example, it should look like:

{{1,1},{2,0},{2,0},{3,2},{3,248},{3,234},{3,5},{3,6}...}

I have tried something like merge partition function but did not help.

What kind of function should I use?

-

## marked as duplicate by Kuba, Louis, MarcoB, PlatoManiac, Shutao TANGFeb 24 at 2:37

@Kuba MapThread[Prepend,{Flatten[b],a}] ? – Peter Roberge Feb 23 at 18:07
@Kuba I think OP wants the result to be a list of 2-element lists. PeterRoberge, I don't think that code works when the lengths of Flatten[b] and a are different, and moreover if the lengths were the same we would be trying to prepend onto an atomic expression (Number), which probably would give an error. – C. Woods Feb 23 at 18:11
@PeterRoberge wasn't paying attention :) – Kuba Feb 23 at 18:12

Try this:

Flatten[MapThread[Outer[({#1, #2}&), #1, #2]&, {List /@ a, b}], 2]

a should be a list, b should be a list of lists.

-
MapThread[Function[{x}, {#1, x}] /@ #2 &, {a, b}]~Flatten~1 – march Feb 23 at 18:11
@march This is much nicer, though I admit I don't like the inline notation. I'm a beginner myself to the Wolfram language, so I appreciate seeing new techniques like this, thank you. – C. Woods Feb 23 at 18:16
I think you'll learn to love it, eventually. We all do :) In any case, here's the more verbose version: Flatten[MapThread[Map[Function[{x}, {#1, x}], #2] &, {a, b}], 1] – march Feb 23 at 18:53
@march. "we all do" -- I don't, so speak for yourself only. – m_goldberg Feb 23 at 22:01
@m_goldberg. Well, okay. One person's readability is another person's obfuscation. Check out Simon Woods' answer! – march Feb 23 at 23:23
-

This solution is conceptually similar to that of C. Woods, except that it uses PadLeft[] + Transpose[] to do the job:

a = {1, 2, 3};
b = {{1}, {0, 0}, {2, 248, 234, 5, 6}};
{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
-

With @J.M's a and b:

a = {1, 2, 3};
b = {{1}, {0, 0}, {2, 248, 234, 5, 6}};

maybe something like:

{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
-
Cases[Thread /@ Flatten[{a, b}, {{2}, {1}}], {_, _}, {-2}]

{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}

Thread /@ Flatten[{a, b}, {{2}, {1}}]

{{{1, 1}}, {{2, 0}, {2, 0}}, {{3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}, {4}, {5}, {6}}

Edit

Using the suggestion made by garej in a comment:

Select[#, ListQ] & @ Flatten[#, 1] & @ (Thread /@ Flatten[#, {{2}, {1}}] &) @ {a, b}

{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}

I assumed the lists to be 'ragged', hence the use of Flatten

a = {1, 2, 3, 4, 5, 6}
b = { {1}, {0, 0}, {2, 248, 234, 5, 6}}
-
nice use of Flatten as transposing tool +1; Cases for exotica here? :)) aslo Flatten[Thread /@ Flatten[{a, b}, {{2}, {1}}], 1] – garej Feb 24 at 6:01