Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Kuba, Louis, MarcoB, PlatoManiac, Shutao TANG Feb 24 at 2:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@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.

share|improve this answer
2  
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
1  
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
2  
@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
Thread@*List~MapThread~{a, b}~Flatten~1
share|improve this answer

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}};
Flatten[MapThread[Transpose[PadLeft[{#1}, {2, Automatic}, #2]] &, {b, a}], 1]
{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
share|improve this answer

With @J.M's a and b:

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

maybe something like:

Thread[Flatten /@ {Table[#1, #2] & @@@ Thread[{a, Length /@ b}], b}]
{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
share|improve this answer
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}}
share|improve this answer
1  
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.