# Creating a new list from an existing one

Suppose we have a list containing four sub-lists like that

data = {
{{1, -1}, {2, 5}, {3, 9}, {4, 2}},
{{1, -4}, {2, 0}, {3, 11}, {4, -1}},
{{1, 3}, {2, 2}, {3, 22}, {4, 1}},
{{1, 0}, {2, 0}, {3, -5}, {4, 7}}
};


Now I want to create a new list, data2, containing the following

d1 = data[[1]];
d2 = data[[2]];
d3 = data[[3]];
d4 = data[[4]];
d11 = d1[[All, 1]];
d22 = d1[[All, 2]];
d33 = d2[[All, 2]];
d44 = d3[[All, 2]];
d55 = d4[[All, 2]];
data2 = Table[{d11[[i]], d22[[i]], d33[[i]], d44[[i]], d55[[i]]}, {i, 1, Length[d11]}]


which gives

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

I am pretty sure that there must be a quicker and much more elegant way of obtaining the new list.

Any suggestions?

In:

m = Map[Last, data, {2}];
v = First /@ First@data;


Out:

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1,
7}}

• Very nice and quick! May 6 '17 at 15:52
Transpose[Prepend[data[[All, All, 2]], data[[1, All, 1]]]]


or

Flatten[{{data[[1, All, 1]]}, data[[All, All, 2]]}, {{3}, {1, 2}}]


or

Drop[Flatten[data, {{2}, {1, 3}}], {}, {3, -2, 2}]

• Nice! Let's wait for a while to see if someone else has anything else to suggest. May 6 '17 at 15:19
• In your last one you could flatten levels 1 and 3 the other way and so put all the dropped elements together Flatten[data, {{2}, {3, 1}}][[All, 4 ;;]] May 6 '17 at 22:36

here you are

l1 = Length@data;
l2 = First[Length /@ data];
Table[d = Table[Join[data[[i]][[j]]], {i, 1, l1}];
Drop[Join[First /@ d, Last /@ d], l1 - 1], {j, 1, l2}]


{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

• It works only when four sub-lists are present. I tested it in a case with 1000 lists and it failed. May 6 '17 at 15:27
• I think you are wrong. I never used the number 4. I used length@data May 6 '17 at 15:30
• Take for example the list data = { {{1, -1}, {2, 5}, {3, 9}, {4, 2}, {5, 1}}, {{1, -4}, {2, 0}, {3, 11}, {4, -1}, {5, 1}}, {{1, 3}, {2, 2}, {3, 22}, {4, 1}, {5, 1}}, {{1, 0}, {2, 0}, {3, -5}, {4, 7}, {5, 1}} }; The method proposed buy Coolwater gives the correct answer, while yours is not correct. May 6 '17 at 15:40
• I fixed it... it was working for nxn May 6 '17 at 15:43
• Now it works fine! May 6 '17 at 15:45
Join[{#[[1]]}, #2] & @@@ Transpose[data, {3, 1, 2}]


{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

Join[#[[1]], #[[2 ;;, 2]]] & /@ (Transpose@data)


{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

I found this line of thought most intuitive:

Transpose[Join[data[[{1}, ;; , 1]], data[[;; , ;; , 2]]]]


If you would call all your pairs {x,y} then this takes all ys and prepends a single x. This is already the transposed version of what you want.

It is very similar to the first option provided by coolwater, but perhaps more people intuitively understand Join than Prepend.

Drop[#, {3, 8, 2}] & /@ Partition[Flatten[Transpose@data], 8]
`