# How to join two one dimension lists as columns in a matrix [duplicate]

I would like to take a one dimensional list, e.g., list1={1,2,3} and add a second column, e.g., list2={a,b,c} to get list3={{1,a},{2,b},{3,c}}.

I know that one can add a column to a matrix using Transpose and Join, but this doesn't seem to work for a simple list.

• list1 = {1, 2, 3}; list2 = {a, b, c}; {list1, list2} // Transpose or Thread Commented Jun 2, 2023 at 0:25
• columnAttach[{1, 2, 3}, {a, b, c}] from here Commented Jun 2, 2023 at 1:11
• Commented Jun 2, 2023 at 2:29

Let's try to do 10 ways.

Courtesy of @cvgmt in the comments under the OP

{list1, list2} // Transpose


Inner[List, list1, list2, List]
ArrayReduce[Dot, {list1, list2}, 1]
ArrayReshape[Riffle[list1, list2], {Length@list1, 2}]
Table[{list1[[i]], list2[[i]]}, {i, 1, Length@list1}]


Another great recommendation by @cvgmt as a comment under this answer

Outer[List, list1, list2] // Diagonal


@Nasser's suggested solution

MapThread[{#1, #2} &, {list1, list2}]


@Michael E2 used J. M.'s function in the following way

columnAttach[{1, 2, 3}, {a, b, c}]


All of the above give

• Or Outer[List, list1, list2] // Diagonal Commented Jun 2, 2023 at 1:02
• I do not know if this counts different that one already given using MapThread. MapThread[{#1, #2} &, {list1, list2}] Commented Jun 2, 2023 at 1:51
• @cvgmt that's a very nice suggestion. Why don't you make it an answer with the other two ways in the comment under the OP?
– bmf
Commented Jun 2, 2023 at 3:41
• may be you could collect these into your answer and see if we got 10 or not. I think we have 10 different ones already scattered in comments and in your answer.. Commented Jun 2, 2023 at 3:56
• Not meaning to offend anyone, but with regard to Outer I don’t think we should promote using $O(n^2)$ operations for $O(n)$ tasks. Commented Jun 2, 2023 at 12:30

Hmm, haven't seen this one yet:

Flatten[{list1, list2}, {2}]

• Thank you for pointing this out! I keep forgetting about this use of Flatten
– bmf
Commented Jun 3, 2023 at 3:57

Here we go again with another way:

Values@GroupBy[Join @@ (PositionIndex[#] & /@ {list1, list2}), Key, Keys]

(*{{1, a}, {2, b}, {3, c}}*)


Also:

Partition[Riffle[list1, list2], {Length@{list1, list2}}]

• 12 and counting! Very nice :-)
– bmf
Commented Jun 2, 2023 at 5:28