7
$\begingroup$

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}

$\endgroup$
6
  • 2
    $\begingroup$ Have a look at Sequence. $\endgroup$ Sep 4 at 16:43
  • $\begingroup$ @b.gates.you.know.what I also tried Sequence [x,y,z,p] but this does not work either $\endgroup$
    – John
    Sep 4 at 16:48
  • $\begingroup$ 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. $\endgroup$ Sep 4 at 16:56
  • $\begingroup$ @HighPerformanceMark I have updated the question to include exactly what I want and the full form of what I want to convert $\endgroup$
    – John
    Sep 4 at 17:05
  • 3
    $\begingroup$ Well the easy answer is simply to move the Flatten outside the Insert, but I expect that isn't what you want, is it? $\endgroup$ Sep 4 at 17:10
8
$\begingroup$
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 *)
$\endgroup$
4
$\begingroup$

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} *)
$\endgroup$
1
$\begingroup$
 Riffle[list,#,{4,4+Length[#],1}]&[lst2]

 (* {36.3019, 36.9458, 37.983, 30, 40, 50, 60, 30, 38.9583, 39.2301, 40.0107} *)
$\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.