# How to interleave data in a certain way

Here's the deal: I have a table that has the form

 list = {{A,{a,b,c}},{B,{d,e,f}},{C,{g,h,i,j}}}


and so on, where the innermost lists can have an arbitary number of elements that differs from list to list, and I'd like it to look like

{{A,a},{A,b},{A,c},{B,d},{B,e},{B,f},{C,g},{C,h},{C,i},{C,j}}


and so on. How do I do this?

• Join @@ (Thread /@ list) Commented Nov 24, 2015 at 15:21
• My first answer was the following monstrosity: Flatten[Inner[Outer[##] /. x_[y_] :> {x, y} &, Sequence @@ Transpose[l], List], 1] Commented Nov 24, 2015 at 15:27
• @PatrickStevens How nice! :) Commented Nov 24, 2015 at 15:30
• @belisariushassettled I am definitely of the "add more stuff until it works" school of programming. Commented Nov 24, 2015 at 15:31
• @PatrickStevens The Mathematica environment somehow encourages that. It is unbelievable how much one can entangle a simple problem by following a line of thought instead of giving it a break and think again! Commented Nov 24, 2015 at 15:34

Here's a simple one-liner:

Apply[Join]@*Map[Thread]@list

(* {{A, a}, {A, b}, {A, c}, {B, d}, {B, e}, {B, f}, {C, g}, {C, h}, {C, i}, {C, j}} *)

• Love the mix of operator forms and composition. Commented Nov 24, 2015 at 16:52

Since V10.0 you can use the following clear expression

Catenate[Thread /@ list]
(* {{A, a}, {A, b}, {A, c}, {B, d}, {B, e}, {B, f}, {C, g}, {C, h}, {C,
i}, {C, j}} *)

• I checked that timings of Catenate[...] and Faltten[...,1] are almost the same. Commented Nov 24, 2015 at 16:17
• +1 I always forget about Catenate! Commented Nov 24, 2015 at 17:15