# Insert a list within another without extra brackets

I have a list1:

list1={{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}}



and list2:

 list2={{2, -0.6}, {3, -0.6}}


and I would like insert list2 at position 3 in list1 to get:

 {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4, 0}, {10, -0.4}}


But when I use Insert[list1, list2, 3], I get an extra pair of brackets from 'list2:

{{1, -1.1}, {2, -1}, {{2, -0.6}, {3, -0.6}}, {3, -0.9}, {4, 0}, {10, -0.4}}


Is it possible do it without the extra brackets?

• version 12.1 or later
list1 = {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}};
list2 = {{2, -0.6}, {3, -0.6}};
Insert[list1, Splice@list2, 3]


{{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4, 0}, {10, -0.4}}

• another way.
FlattenAt[Insert[list1, list2, 3], 3]

• Thanks, but Mathematica shows the error: Splice::string: String expected at position 1 in Splice[{{2,-0.6},{3,-0.6}}]. Commented Oct 31, 2022 at 12:17
• @MamMam Needs version 12.1 or later. Another way Join[list1[[1 ;; 2]], list2, list1[[3 ;; -1]]] Commented Oct 31, 2022 at 12:24
• For versions before 12.1, for simple cases (not this one), you may be able to replace the Splice@ with Sequence@@. Commented Oct 31, 2022 at 14:20
• @GustavoDelfino I have test Sequence@@ befor I post the answer. It does not work. Commented Oct 31, 2022 at 14:24

Using Sequence (for older versions of WL without Splice):

list1 = {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}};
list2 = {{2, -0.6}, {3, -0.6}};
Insert[list1, Inactive[Sequence] @@ list2, 3] // Activate

• A similar solution: Insert[list1, \[FormalG] @@ list2, 3] /. \[FormalG] -> Sequence (\[FormalG] can be replaced with any undefined variable) Commented Oct 31, 2022 at 16:56
• Unevaluated[Sequence @@ list2]
– att
Commented Oct 31, 2022 at 19:30

The theme of this answer ended up being : "playing with Sequence and MapAt"

• Using MapAt, Apply, Sequence and Insert:

MapAt[Apply[Sequence], Insert[list1, list2, 3], 3]

(* {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4,
0}, {10, -0.4}}  *)

• Using MapAt, Sequence, Apply, Join, List and Function (no Insert):

MapAt[Sequence @@ list2~Join~{#} &, list1, 3]

(* {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4,
0}, {10, -0.4}} *)

• Using MapAt, Construct, Sequence, Apply, and Function (no Insert):

MapAt[Construct[Sequence, Sequence @@ list2, #] &, list1, 3]

(* {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4,
0}, {10, -0.4}} *)


but

 MapAt[Sequence[Sequence @@ list2, #] &, list1, 3]


EDIT

As mentioned by @att in the comments, the error is due to the fact that Function does not have the attribute SequenceHold. For workarounds for this issue see the answers here. Some of the workarounds from that question including the one by @att applied to this case are:

• Using MapAt, Sequence (x2), Apply, and Function(x2) (no Insert):

  MapAt[##&[Sequence @@ list2, #] &, list1, 3]

(* {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4,
0}, {10, -0.4}}  *)

• Using MapAt, Sequence (x2), Apply(x2), and Function (no Insert):

 MapAt[Sequence @@ {Sequence @@ list2, #} &, list1, 3]

(* {{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4,
0}, {10, -0.4}} *)

• The last one fails because Function does not have the SequenceHold attribute. You can use ##& for the outer Sequence instead.
– att
Commented Oct 31, 2022 at 19:50
• @att nice I saw the other answer where you used that technique. I supposes that it works as it defers the evaluation as with Sequence @@ {Sequence @@ list2, #} & Commented Oct 31, 2022 at 20:32
a = {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}};

b = {{2, -0.6}, {3, -0.6}};


Insert at position:

p = 3;

c = Insert[a, b, p];


Using Replace and Splice (new in 12.1)

Replace[c, x : {__List} :> Splice[x], {1}]


Using ReplaceAt (new in 13.1)

ReplaceAt[x_ :> Splice[x], p] @ c


Using Take

Catenate[{Take[a, {1, p - 1}], b, Take[a, {p, -1}]}]


All produce

{{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4, 0}, {10, -0.4}}

list1 = {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}}
list2 = {{2, -0.6}, {3, -0.6}}

TakeList[list1, {2, All}] // Insert[#, list2, 2] & //
Map[Apply[Sequence]]

Insert[list1, Hold[Sequence @@ list2], 3] // ReleaseHold

SequenceReplace[list1, {a_, b_, c___} :>
Sequence[a, b, Sequence @@ list2, c]]

SequenceSplit[
Insert[list1, \[Wolf], 3], {\[Wolf]} :> Sequence @@ list2]

l1 = {{1, -1.1}, {2, -1}, {3, -0.9}, {4, 0}, {10, -0.4}};

l2 = {{2, -0.6}, {3, -0.6}};


Using Riffle:

f = Flatten[{#1[[;; 2]], #2, #3[[3 ;;]]}, 1] &;

f @@ Activate@Riffle[Array[Inactive@l1 &, Length@l2], Inactive@l2]


{{1, -1.1}, {2, -1}, {2, -0.6}, {3, -0.6}, {3, -0.9}, {4, 0}, {10, -0.4}}`