I have two lists l1={a,b,c} and l2={{a1},{b1,b2},{c1,c2,c3}}. What kind of operations can I use to get a list like l={{a,a1},{b,b1},{b,b2},{c,c1},{c,c2},{c,c3}}?

I tried to use Thread to get around, but I failed. Any suggestions?

Update: I also have a function f, in the end I want to arrive at something like f@@@l={f[a, a1], f[b, b1], f[b, b2], f[c, c1], f[c, c2], f[c, c3]}. I can do that with Table as follows:

Flatten@Table[f[l1[[i]], #] & /@ l2[[i]], {i, Length@l1}]

But I wonder whether there are other ways.

  • $\begingroup$ @Kuba Thanks for the link. What am I supposed to do with my post then? Shall I close it or delete it? $\endgroup$
    – hxiao
    Nov 16 '15 at 7:37
  • $\begingroup$ Posts closed as a duplicate are left here as a road sign for future visitors. And since it's not easy to find such duplicates, that is desired. :)(so do not delete it please :)) $\endgroup$
    – Kuba
    Nov 16 '15 at 7:39

For the first request:

Flatten[Thread /@ Thread[{l1, l2}], 1]

For the second request, you just need Listable:

SetAttributes[f, Listable]
Flatten@f[l1, l2]

OK, actually the first request can also be satisfied by setting Listable attribute:

g = Function[{a, b}, {a, b}, Listable]
Flatten[g[l1, l2], 1]

Here are some ways you could use Thread to achieve your goal:

l1 = {a, b, c} ;
l2 = {{a1}, {b1, b2}, {c1, c2, c3}};
Join @@ (Thread[{#1, #2}] & @@@ Thread[{l1, l2}])
Join @@ MapThread[Thread[{#1, #2}] &, {l1, l2}]
Inner[Sequence @@ Thread[{#1, #2}] &, l1, l2, List]

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