# Flatten simple list

I know this is probably a very silly question but I am trying to get {x,y,z,p} to be x,y,z,p.

For this I am using Flatten in this way:

Flatten[{x, y,z,p}]
Flatten @@@ {x, y,z,p}


but it does not work. How can I do this?

NOTE: What I want to do is the following:

Give a list such as:

list={36.3019, 36.9458, 37.983, 38.9583, 39.2301, 40.0107};


I want to add four numbers (30.40.50,60) into the 4 position such:

Insert[List, Flatten[{30, 40, 50, 60}], 4]


The problem I am having is that these numbers are not flatten and I get: {36.3019, 36.9458, 37.983, {30, 40, 50, 60}, 38.9583, 39.2301, 40.0107}

• Have a look at Sequence. Commented Sep 4, 2021 at 16:43
• @b.gates.you.know.what I also tried Sequence [x,y,z,p] but this does not work either
– John
Commented Sep 4, 2021 at 16:48
• What are you trying to do? What is the FullForm of the thing you want to convert List[x,y,z,p]into? @b.gates.you.know.what has already suggested using Sequence which is very useful when you want to splice elements into a List or an argument list without changing the structure. But everything in Mathematica is an expression, and it's not clear what kind of expression you want to convert your list into. Commented Sep 4, 2021 at 16:56
• @HighPerformanceMark I have updated the question to include exactly what I want and the full form of what I want to convert
– John
Commented Sep 4, 2021 at 17:05
• Well the easy answer is simply to move the Flatten outside the Insert, but I expect that isn't what you want, is it? Commented Sep 4, 2021 at 17:10

Clear["Global*"]

list1 = {36.3019, 36.9458, 37.983, 38.9583, 39.2301, 40.0107};

list2 = {30, 40, 50, 60};


There are many ways to do what you want; for example,

res[1] = Insert[list1, list2, 4] // Flatten

(* {36.3019, 36.9458, 37.983, 30, 40, 50, 60, 38.9583, 39.2301, 40.0107} *)

res[2] = Join[list1[[1 ;; 3]], list2, list1[[4 ;;]]];

res[3] = {list1[[1 ;; 3]], list2, list1[[4 ;;]]} // Flatten;

res[4] = Insert[list1, $$a, 4] /.$$a :> Sequence @@ list2;

res[5] = Insert[list1, Inactive[Sequence] @@ list2, 4] // Activate;

res[6] = Insert[list1, Hold[Sequence @@ list2], 4] // ReleaseHold;

res[7] = Insert[list1, Splice[list2],
4]; (* Splice is experimental and is new in v12.1 *)

res[8] = Catenate[{list1[[1 ;; 3]], list2, list1[[4 ;;]]}];


All produce identical results

SameQ @@ (res /@ Range[8])

(* True *)


One way is to use FlattenAt with the same position argument as that of Insert:

FlattenAt[Insert[list, {30, 40, 50, 60}, 4], 4]
(* {36.3019, 36.9458, 37.983, 30, 40, 50, 60, 38.9583, 39.2301, 40.0107} *)

 Riffle[list,#,{4,4+Length[#],1}]&[lst2]

(* {36.3019, 36.9458, 37.983, 30, 40, 50, 60, 30, 38.9583, 39.2301, 40.0107} *)

a = {36.3019, 36.9458, 37.983, 38.9583, 39.2301, 40.0107};

b = {30, 40, 50, 60};

Flatten @ {Take[a, 4], b, Take[a, 4 - Length[a]]}


{36.3019, 36.9458, 37.983, 38.9583, 30, 40, 50, 60, 39.2301, 40.0107}

list1 = {36.3019, 36.9458, 37.983, 38.9583, 39.2301, 40.0107};

list2 = {30, 40, 50, 60};


Another way using ReplacePart:

ReplacePart[list1, {4} :> Splice@*Append @@ {list2, list1[[4]]}]


{36.3019, 36.9458, 37.983, 30, 40, 50, 60, 38.9583, 39.2301, 40.0107}`