10
$\begingroup$

Say a list is given as

list = {a, b, c, d, r, m, n};

Suppose I want to insert 2 and 3 at position 3 and 7, respectively.

Insert[list, 2, 3] puts 2 at position 3, but Insert[list, {{2, 3}, {3, 7}}] does nothing.

How is it possible?

$\endgroup$
4

7 Answers 7

7
$\begingroup$

Look what I found after spelunking:

GroupTheory`PermutationGroups`Private`FoldInsert[
 {a, b, c, d, r, m, n},
 {2, 3}, {3, 7}
 ]

{a, b, 2, c, d, r, 3, m, n}

Well, it is not entirely correct for it does not revert the order of insertion... =/

$\endgroup$
4
  • $\begingroup$ Wow! $\phantom{}$ $\endgroup$ Commented Sep 27, 2018 at 14:53
  • 1
    $\begingroup$ I wonder if this package has similar functions. $\endgroup$ Commented Sep 27, 2018 at 15:16
  • $\begingroup$ @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...) $\endgroup$ Commented Sep 27, 2018 at 15:26
  • $\begingroup$ @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago. $\endgroup$ Commented Sep 27, 2018 at 16:20
7
$\begingroup$

How about this

myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &, 
                                       list, 
                                       SortBy[valuePosList, -Last[#] &]
                                      ]

myInsert[list, {{2, 3}, {3, 7}}]
{a, b, 2, c, d, r, m, 3, n}
$\endgroup$
5
$\begingroup$
insertList[list_, valuePosList_] := ReplacePart[
  list,
  Apply[
   Rule[#2, Sequence[#1, list[[#2]]]] &,
   valuePosList
   , {1}
   ]
  ]

Mathematica graphics

$\endgroup$
5
$\begingroup$
list = {a, b, c, d, r, m, n};
elems = {{2, 3}, {3, 7}};

Fold[Insert[#1, First@#2, Last@#2] &, list, SortBy[elems, First]]

{a, b, 2, c, d, r, 3, m, n}

$\endgroup$
3
$\begingroup$
list = {a, b, c, d, r, m, n};

p = {3, 7};
v = {2, 3};

Using Riffle and TakeList (new in 11.2)

take = Flatten[{{First[#], Differences[#]} - 1, All}] & [Sort @ p]

{2, 3, All}

Flatten @ Riffle[TakeList[list, take], v]

{a, b, 2, c, d, r, 3, m, n}

$\endgroup$
2
  • $\begingroup$ There needs to be a sorting clause as p={7,3} results in an unevaluated form. Can you please modify it? $\endgroup$
    – Syed
    Commented Apr 12 at 7:01
  • $\begingroup$ Thanks, Syed, I updated the answer $\endgroup$
    – eldo
    Commented Apr 12 at 7:08
3
$\begingroup$
Query[ReverseSort[{3 -> (Splice[{2, #}] &), 7 -> (Splice[{3, #}] &)}]]@list

(* {a, b, 2, c, d, r, 3, m, n} *)
$\endgroup$
3
$\begingroup$

A variant of @Syed's answer using Fold:

 InsertList[list1_, list2__List /; AllTrue[{list2}, Length[#] == 2 &]] := 
  Module[{modlist}, 
  modlist = Fold[Insert[#1, Sequence @@ #2] &, list1, SortBy[{list2}, Last]];
  modlist]

Testing InsertList:

l = {a, b, c, d, r, m, n};

InsertList[l, {2, 3}, {3, 7}]

{a, b, 2, c, d, r, 3, m, n}

$\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.