3
$\begingroup$

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.

$\endgroup$
3
  • 3
    $\begingroup$ list1 = {1, 2, 3}; list2 = {a, b, c}; {list1, list2} // Transpose or Thread $\endgroup$
    – cvgmt
    Commented Jun 2, 2023 at 0:25
  • $\begingroup$ columnAttach[{1, 2, 3}, {a, b, c}] from here $\endgroup$
    – Michael E2
    Commented Jun 2, 2023 at 1:11
  • 1
    $\begingroup$ See List Manipulation $\endgroup$
    – Bob Hanlon
    Commented Jun 2, 2023 at 2:29

3 Answers 3

4
$\begingroup$

Let's try to do 10 ways.

Courtesy of @cvgmt in the comments under the OP

{list1, list2} // Transpose
{list1, list2} // Thread

My original answer

Inner[List, list1, list2, List]
ArrayReduce[Dot, {list1, list2}, 1]
ArrayReshape[Riffle[list1, list2], {Length@list1, 2}]
MapThread[List, {list1, list2}]
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

res

$\endgroup$
7
  • 2
    $\begingroup$ Or Outer[List, list1, list2] // Diagonal $\endgroup$
    – cvgmt
    Commented Jun 2, 2023 at 1:02
  • 1
    $\begingroup$ I do not know if this counts different that one already given using MapThread. MapThread[{#1, #2} &, {list1, list2}] $\endgroup$
    – Nasser
    Commented Jun 2, 2023 at 1:51
  • $\begingroup$ @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? $\endgroup$
    – bmf
    Commented Jun 2, 2023 at 3:41
  • 1
    $\begingroup$ 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.. $\endgroup$
    – Nasser
    Commented Jun 2, 2023 at 3:56
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jun 2, 2023 at 12:30
3
$\begingroup$

Hmm, haven't seen this one yet:

Flatten[{list1, list2}, {2}]
$\endgroup$
1
  • $\begingroup$ Thank you for pointing this out! I keep forgetting about this use of Flatten $\endgroup$
    – bmf
    Commented Jun 3, 2023 at 3:57
2
$\begingroup$

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}}]
$\endgroup$
1
  • 1
    $\begingroup$ 12 and counting! Very nice :-) $\endgroup$
    – bmf
    Commented Jun 2, 2023 at 5:28

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