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$
  • $\begingroup$ Flatten /@ Transpose[{myList, data}] ? $\endgroup$ – user1066 Jul 8 '18 at 19:53
3
$\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}}

| improve this answer | |
$\endgroup$
4
$\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}}

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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