6
$\begingroup$

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?

$\endgroup$

6 Answers 6

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

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
$\endgroup$
2
  • $\begingroup$ A similar solution: Insert[list1, \[FormalG] @@ list2, 3] /. \[FormalG] -> Sequence (\[FormalG] can be replaced with any undefined variable) $\endgroup$ Commented Oct 31, 2022 at 16:56
  • 2
    $\begingroup$ Unevaluated[Sequence @@ list2] $\endgroup$
    – att
    Commented Oct 31, 2022 at 19:30
5
$\begingroup$

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]

leads to an error

error

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}} *)
    
$\endgroup$
2
  • $\begingroup$ The last one fails because Function does not have the SequenceHold attribute. You can use ##& for the outer Sequence instead. $\endgroup$
    – att
    Commented Oct 31, 2022 at 19:50
  • $\begingroup$ @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, #} & $\endgroup$ Commented Oct 31, 2022 at 20:32
3
$\begingroup$
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}}

$\endgroup$
3
$\begingroup$
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]
$\endgroup$
3
$\begingroup$
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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.