# Joining two lists element by element [closed]

If I have two sets of data say, x={1,2,3} and y=(4,5,6}, and I wanted to make a table T = {{1,4},{2,5},{3,6}}, how would I do so by using x and y and not just typing out the whole table? Is there any way that I can make a table by doing something like Table[x,y] after specifying the vectors x and y?

I'm asking because I want to reference the x values when I plot a fitted line.

• x = {1, 2, 3}; y = {4, 5, 6}; m2 = Transpose[{x, y}] Jan 30 at 1:50
• Thread[{x, y}] Inner[List, x, y, List] Jan 30 at 1:57
• Almost certainly, this is a duplicate. Where is it? Jan 30 at 23:38
• Ah here it is: mathematica.stackexchange.com/questions/101769/…. Jan 30 at 23:39
• Does this answer your question? How to make a table from two lists? Jan 30 at 23:40

Some ways of doing it:

With

x = {1, 2, 3};
y = {4, 5, 6};


all of the following

Thread[{x, y}] // MatrixForm
Transpose[{x, y}] // MatrixForm
Inner[List, x, y, List] // MatrixForm
Table[{x[[i]], y[[i]]}, {i, 1, Length[x]}] // MatrixForm
Partition[Riffle[x, y], 2] // MatrixForm
Transpose[ArrayFlatten[{x, y}], {2, 1}] // MatrixForm
Function[, {##}, Listable][x, y] // MatrixForm
Quiet@Factor[x, y] /. Factor -> List // MatrixForm


a variant of the last is:

foo[x, y] /. foo -> List // MatrixForm


Edit 1: there'a a new command called ArrayReduce

ArrayReduce[Dot, {x, y}, 1] // MatrixForm


Edit 2: and another way

Multicolumn[Flatten@ArrayReshape[{x, y}, {2, 3}],
2][[1]] // MatrixForm


Edit 3: special thanks to @user1066 for the valuable comment. One can observe how the following behaves

ArrayReduce[f, {x, y}, 1] // MatrixForm


and from the above we can deduce that the following two commands give the desired output

ArrayReduce[Join, {x, y}, 1] // MatrixForm
ArrayReduce[Union, {x, y}, 1] // MatrixForm


All of the above give:

• @user1066 many thanks for the feedback. the comment is implemented in the answer
– bmf
Jan 31 at 2:13

This is what you have

(mat = {{1, 2, 3},{4, 5, 6}}) // MatrixForm


And this is what you want

(matWanted = {{1, 4}, {2, 5}, {3, 6}}) // MatrixForm


Now if you have the first form, how to obtain the second form from it? What is the name of the operation to do it?

Another way:

Diagonal@Outer[List, x, y] // MatrixForm


• Number 10!!! Wonderful
– bmf
Jan 30 at 4:34
• The job is done, mate! Jan 30 at 4:35

No one seems to have mentioned Flatten

Flatten[{x,y},{{2}}]

(* {{1, 4}, {2, 5}, {3, 6}} *)


When dealing with 'ragged' arrays, this method may be advantageous:

Compare:

Flatten[{x, Append[y, 100]},{{2}}]

Transpose[{x, Append[y, 100]}]

(*
{{1, 4}, {2, 5}, {3, 6}, {100}}

Transpose::nmtx: The first two levels of {{1, 2, 3}, {4, 5, 6, 100}} cannot be transposed.

*)


The documentation for Flatten also gives an example of how to 'Do a "transpose" on a ragged array', and this usage of Flatten is very well explained here by Leonid Shifrin

• Very nice! I knew I missed something elementary. Thanks :-)
– bmf
Jan 30 at 11:34