3
$\begingroup$

I have two lists:

{x1, x2, x3, x4, x5} 

and

{y1, y2, y3, y4, y5}

How can I get a list like the one below from them, where each element in the first list is paired with each element in the second?

{{x1, y1}, {x1, y2}, {x1, y3}, ..., {x2, y1}, {x2, y2}, ..., {x5, y5}}

Can I avoid using Table to do it?

$\endgroup$

closed as off-topic by Quantum_Oli, corey979, Kuba, Feyre, MarcoB Nov 24 '16 at 15:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Quantum_Oli, corey979, Kuba, Feyre, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Outer is made for this. Here, Outer[List, xlist, ylist] does the job. $\endgroup$ – Marius Ladegård Meyer Nov 24 '16 at 11:13
2
$\begingroup$

Use Tuples:

Tuples[{{x1, x2, x3, x4, x5} , {y1, y2, y3, y4, y5}}]
(* {{x1, y1}, {x1, y2}, {x1, y3}, {x1, y4}, {x1, y5}, {x2, 
  y1}, {x2, y2}, {x2, y3}, {x2, y4}, {x2, y5}, {x3, y1}, {x3, 
  y2}, {x3, y3}, {x3, y4}, {x3, y5}, {x4, y1}, {x4, y2}, {x4, 
  y3}, {x4, y4}, {x4, y5}, {x5, y1}, {x5, y2}, {x5, y3}, {x5, 
  y4}, {x5, y5}} *)

Can I avoid using Table to do it?

Actually I don't think Table is bad for this if you use the right syntax. I don't think it's any worse than Outer.

xx = {x1, x2, x3, x4, x5};

yy = {y1, y2, y3, y4, y5};

Table[{x, y}, {x, xx}, {y, yy}]
(* {{{x1, y1}, {x1, y2}, {x1, y3}, {x1, y4}, {x1, y5}}, {{x2, 
   y1}, {x2, y2}, {x2, y3}, {x2, y4}, {x2, y5}}, {{x3, y1}, {x3, 
   y2}, {x3, y3}, {x3, y4}, {x3, y5}}, {{x4, y1}, {x4, y2}, {x4, 
   y3}, {x4, y4}, {x4, y5}}, {{x5, y1}, {x5, y2}, {x5, y3}, {x5, 
   y4}, {x5, y5}}} *)

Catenate[%]
(* {{x1, y1}, {x1, y2}, {x1, y3}, {x1, y4}, {x1, y5}, {x2, 
  y1}, {x2, y2}, {x2, y3}, {x2, y4}, {x2, y5}, {x3, y1}, {x3, 
  y2}, {x3, y3}, {x3, y4}, {x3, y5}, {x4, y1}, {x4, y2}, {x4, 
  y3}, {x4, y4}, {x4, y5}, {x5, y1}, {x5, y2}, {x5, y3}, {x5, 
  y4}, {x5, y5}} *)
$\endgroup$
0
$\begingroup$

There are lots of ways you could achieve this result: the Outer function is probably easiest. For example,

xlist={x1,x2,x3,x4,x5};ylist={y1,y2,y3,y4,y5};
result=Outer[{#1,#2}&,xlist,ylist]

This will have extra layers of braces relative to the output you want: you could get rid of them by applying Flatten[result,1].

$\endgroup$
  • $\begingroup$ Suggest Outer[{#1, #2} &, xlist, ylist] // Catenate, rather than Flatten. $\endgroup$ – m_goldberg Nov 24 '16 at 12:51
  • $\begingroup$ @m_goldberg In older versions Catenate is slower than Join @@ on packed arrays. (I was surprised when people told me about this.) Not sure about Flatten[..., 1]. I do prefer the Catenate syntax. $\endgroup$ – Szabolcs Nov 24 '16 at 12:55

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