To create a list of all the possible combinations of a sublist. I have a list, somewhat resembling the following format:

{{{a1, a2}, {b1}}, {{a1, a2}, {c1, c2}}, {{a1, a2}, {c1, c2}, {d1, d2}}}

I'm trying to get Mathematica to generate a list that is all the combinations of the contents of these sublists. So I'd get

{{a1, b1}, {a2, b1}, {a1, c1}, {a1, c2}, {a2, c1}, {a2, c2}, 
 {a1, c1, d1}, {a1, c1, d2}, {a1 ,c2 ,d1}, {a1, c2, d2}, {a2, c1, d1}, 
 {a2, c1, d2}, {a2, c2, d1}, {a2, c2, d2}}

It was suggested that I use a nested For loop to accomplish this, but I'm not entirely sure that would be the most efficient way to go about this.

Is there a clever way I could apply some combination of the mathematica list functions to accomplish this goal?

  • $\begingroup$ Welcome Andrew. Please look at the help section, especially the section about how to format your question. $\endgroup$
    – halirutan
    Commented Feb 20, 2017 at 2:20
  • 6
    $\begingroup$ Catenate[Tuples /@ list] $\endgroup$
    – yode
    Commented Feb 20, 2017 at 2:43

1 Answer 1


What you want is something nice and clean like this

l = {{{a1, a2}, {b1}}, {{a1, a2}, {c1, c2}}, {{a1, a2}, {c1, c2}, {d1, d2}}};
Join @@ (Tuples /@ l)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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