Suppose I have a list with mixed elements


Is there a way so that I can reshuffle the elements to get the following?


Now, the idea is that I don't know at priori, how many elements there are in the block of {a,b,c,d}, as well as in the block of {e,f,h,i}. What I know that in that block there are elements of the same length: {a,b} is a pair and also the element {e,f} is a pair, but there can be many {a,b} in the first component, and many {e,f} in the second component. I would like to know if there is a way to create an element as the one I wrote before. Maybe also something generalizable to an arbitrary number of elements, like:


going into


I tried playing with Tables and Partitions,but I didn't manage to find a way to get what I want.


1 Answer 1

lst = {{{a, b}, {c, d}}, {{e, f}, {h, i}}};

You can use Tuples or Outer or Distribute:


{{{a, b}, {e, f}}, {{a, b}, {h, i}}, {{c, d}, {e, f}}, {{c, d}, {h, i}}}

Join @@ Outer[List, ## & @@ lst, 1]

{{{a, b}, {e, f}}, {{a, b}, {h, i}}, {{c, d}, {e, f}}, {{c, d}, {h, i}}}

Distribute[lst, List]

{{{a, b}, {e, f}}, {{a, b}, {h, i}}, {{c, d}, {e, f}}, {{c, d}, {h, i}}}

  • $\begingroup$ Thanks! Distribute was exactly the function I was looking for! $\endgroup$ Commented Feb 22, 2020 at 23:11

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.