# How to merge lists? [closed]

For demonstrating purposes suppose I have the following.

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h[n_] := Table[j[(-1)^n, i π/2], {i, 5}];

What I want to do is simply combine the result I get from h[1] and h[2]

For h[1] the output is

{0, -1, 0, 1, 0}

While for h[2] the output is

{1, 0, -1, 0, 1}

I want to merge these two lists so I get

{0, -1, 0, 1, 0, 1, 0, -1, 0, 1}

How can I do it? Or is there perhaps an easier more elegant way to do it, like to tell my function h[n_] to do the case n=1 and n=2 straight away?

• Do you mean to Join them? Oct 4 '17 at 14:23
• I get for your example: h[1]={0.707107, 0., -0.707107, -1., -0.707107} and h[2]={0.707107, 1., 0.707107, 0., -0.707107}?
– mrz
Oct 4 '17 at 14:54
• Yeah Join would have done it as well. @mrz Sorry it should have been π/2 and not π/4. Oct 4 '17 at 15:40
• h[1]~Join~h[2] Oct 4 '17 at 21:44

Join will do the trick:

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h[n_] := Table[j[(-1)^n, i π/2], {i, 5}];

Join[h[1], h[2]]

(* {0, -1, 0, 1, 0, 1, 0, -1, 0, 1} *)

Okey I figured it out. Writing the following does the trick

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h = Table[j[(-1)^n, i π/2], {i, 5}, {n, 2}]
• To get the answer that you requested in the question, you need to change the order of summations and use Flatten, i.e., h = Table[j[(-1)^n, i \[Pi]/2], {n, 2}, {i, 5}] // Flatten Oct 4 '17 at 16:14
j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h = Table[j[(-1)^n, i π/2], {i, 5}, {n, 2}]

Flatten@{Take[h[[All, 1]]], Take[h[[All, 2]]]}

(* {0,-1,0,1,0,1,0,-1,0,1} *)