8
$\begingroup$

Suppose I have a list with mixed elements

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

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

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

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:

{{{a,b},{c,d}},{{e,f},{h,i}},{{l,m},{n,o}}} 

going into

 {{{a,b},{e,f},{l,m}},{{a,b},{h,i},{l,m}},{{a,b},{e,f},{n,o}},{{a,b},{h,i},{n,o}},{{c,d},{e,f},{l,m}},{{c,d},{h,i},{l,m}},{{c,d},{e,f},{n,o}},{{c,d},{h,i},{n,o}}} 

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

$\endgroup$

1 Answer 1

12
$\begingroup$
lst = {{{a, b}, {c, d}}, {{e, f}, {h, i}}}; 

You can use Tuples or Outer or Distribute:

Tuples[lst] 

{{{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}}}

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.