2
$\begingroup$

I have a list of $x,y$ coordinates in a List:

myList:={{0, 1}, {1, 2}, {2, 3}, {3, 4}}

and a corresponding one-dimensional List of data:

data:={5,6,7,8}

I would like to merge these lists such that the $i^{\rm th}$ element of data is added to the $i^{\rm th}$ element of myList, i.e.,

{0, 1, 5}, {1, 2, 6}, {2, 3, 7}, {3, 4, 8}

I tried using Transpose, Join, Flatten, $\ldots$ without success. How can I achieve this?

$\endgroup$
1
  • 1
    $\begingroup$ Flatten /@ Transpose[{myList, data}] ? $\endgroup$
    – user1066
    Jul 8, 2018 at 19:53

2 Answers 2

4
$\begingroup$

Also

MapThread[Append, {myList, data}] (* or *)
MapThread[Join, {myList, List /@ data}]
ArrayPad[myList, {{0}, {0, 1}}, List /@ data]

{{0, 1, 5}, {1, 2, 6}, {2, 3, 7}, {3, 4, 8}}

$\endgroup$
5
$\begingroup$
myList = {{0, 1}, {1, 2}, {2, 3}, {3, 4}};
data = {5, 6, 7, 8};
Join[myList, Partition[data, 1], 2]

{{0, 1, 5}, {1, 2, 6}, {2, 3, 7}, {3, 4, 8}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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