How to insert elements at different locations of a given list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












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?










share|improve this question



















  • 2




    Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
    – J. M. is somewhat okay.♦
    Sep 27 at 13:02










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    Sep 27 at 14:48










  • @Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
    – J. M. is somewhat okay.♦
    Sep 27 at 14:52










  • related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
    – user1066
    Sep 28 at 0:07














up vote
8
down vote

favorite












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?










share|improve this question



















  • 2




    Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
    – J. M. is somewhat okay.♦
    Sep 27 at 13:02










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    Sep 27 at 14:48










  • @Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
    – J. M. is somewhat okay.♦
    Sep 27 at 14:52










  • related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
    – user1066
    Sep 28 at 0:07












up vote
8
down vote

favorite









up vote
8
down vote

favorite











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?










share|improve this question















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?







list-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 at 14:13









Αλέξανδρος Ζεγγ

2,339725




2,339725










asked Sep 27 at 12:52









Soumyajit Roy

1558




1558







  • 2




    Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
    – J. M. is somewhat okay.♦
    Sep 27 at 13:02










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    Sep 27 at 14:48










  • @Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
    – J. M. is somewhat okay.♦
    Sep 27 at 14:52










  • related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
    – user1066
    Sep 28 at 0:07












  • 2




    Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
    – J. M. is somewhat okay.♦
    Sep 27 at 13:02










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    Sep 27 at 14:48










  • @Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
    – J. M. is somewhat okay.♦
    Sep 27 at 14:52










  • related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
    – user1066
    Sep 28 at 0:07







2




2




Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
– J. M. is somewhat okay.♦
Sep 27 at 13:02




Dirty trick: Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
– J. M. is somewhat okay.♦
Sep 27 at 13:02












@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
– Henrik Schumacher
Sep 27 at 14:48




@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
– Henrik Schumacher
Sep 27 at 14:48












@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
– J. M. is somewhat okay.♦
Sep 27 at 14:52




@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
– J. M. is somewhat okay.♦
Sep 27 at 14:52












related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
– user1066
Sep 28 at 0:07




related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
– user1066
Sep 28 at 0:07










3 Answers
3






active

oldest

votes

















up vote
7
down vote













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






share|improve this answer





























    up vote
    5
    down vote













    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... =/






    share|improve this answer




















    • Wow! $phantom$
      – J. M. is somewhat okay.♦
      Sep 27 at 14:53







    • 1




      I wonder if this package has similar functions.
      – Î‘λέξανδρος Ζεγγ
      Sep 27 at 15:16











    • @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
      – Henrik Schumacher
      Sep 27 at 15:26











    • @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
      – Î‘λέξανδρος Ζεγγ
      Sep 27 at 16:20

















    up vote
    2
    down vote













    insertList[list_, valuePosList_] := ReplacePart[
    list,
    Apply[
    Rule[#2, Sequence[#1, list[[#2]]]] &,
    valuePosList
    , 1
    ]
    ]


    Mathematica graphics






    share|improve this answer




















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182704%2fhow-to-insert-elements-at-different-locations-of-a-given-list%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote













      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






      share|improve this answer


























        up vote
        7
        down vote













        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






        share|improve this answer
























          up vote
          7
          down vote










          up vote
          7
          down vote









          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






          share|improve this answer














          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 27 at 14:02

























          answered Sep 27 at 13:50









          Αλέξανδρος Ζεγγ

          2,339725




          2,339725




















              up vote
              5
              down vote













              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... =/






              share|improve this answer




















              • Wow! $phantom$
                – J. M. is somewhat okay.♦
                Sep 27 at 14:53







              • 1




                I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 15:16











              • @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
                – Henrik Schumacher
                Sep 27 at 15:26











              • @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 16:20














              up vote
              5
              down vote













              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... =/






              share|improve this answer




















              • Wow! $phantom$
                – J. M. is somewhat okay.♦
                Sep 27 at 14:53







              • 1




                I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 15:16











              • @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
                – Henrik Schumacher
                Sep 27 at 15:26











              • @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 16:20












              up vote
              5
              down vote










              up vote
              5
              down vote









              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... =/






              share|improve this answer












              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... =/







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 27 at 14:50









              Henrik Schumacher

              40.9k258123




              40.9k258123











              • Wow! $phantom$
                – J. M. is somewhat okay.♦
                Sep 27 at 14:53







              • 1




                I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 15:16











              • @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
                – Henrik Schumacher
                Sep 27 at 15:26











              • @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 16:20
















              • Wow! $phantom$
                – J. M. is somewhat okay.♦
                Sep 27 at 14:53







              • 1




                I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 15:16











              • @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
                – Henrik Schumacher
                Sep 27 at 15:26











              • @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
                – Î‘λέξανδρος Ζεγγ
                Sep 27 at 16:20















              Wow! $phantom$
              – J. M. is somewhat okay.♦
              Sep 27 at 14:53





              Wow! $phantom$
              – J. M. is somewhat okay.♦
              Sep 27 at 14:53





              1




              1




              I wonder if this package has similar functions.
              – Î‘λέξανδρος Ζεγγ
              Sep 27 at 15:16





              I wonder if this package has similar functions.
              – Î‘λέξανδρος Ζεγγ
              Sep 27 at 15:16













              @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
              – Henrik Schumacher
              Sep 27 at 15:26





              @ΑλέξανδροςΖεγγ That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
              – Henrik Schumacher
              Sep 27 at 15:26













              @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
              – Î‘λέξανδρος Ζεγγ
              Sep 27 at 16:20




              @HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
              – Î‘λέξανδρος Ζεγγ
              Sep 27 at 16:20










              up vote
              2
              down vote













              insertList[list_, valuePosList_] := ReplacePart[
              list,
              Apply[
              Rule[#2, Sequence[#1, list[[#2]]]] &,
              valuePosList
              , 1
              ]
              ]


              Mathematica graphics






              share|improve this answer
























                up vote
                2
                down vote













                insertList[list_, valuePosList_] := ReplacePart[
                list,
                Apply[
                Rule[#2, Sequence[#1, list[[#2]]]] &,
                valuePosList
                , 1
                ]
                ]


                Mathematica graphics






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  insertList[list_, valuePosList_] := ReplacePart[
                  list,
                  Apply[
                  Rule[#2, Sequence[#1, list[[#2]]]] &,
                  valuePosList
                  , 1
                  ]
                  ]


                  Mathematica graphics






                  share|improve this answer












                  insertList[list_, valuePosList_] := ReplacePart[
                  list,
                  Apply[
                  Rule[#2, Sequence[#1, list[[#2]]]] &,
                  valuePosList
                  , 1
                  ]
                  ]


                  Mathematica graphics







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 27 at 15:55









                  rhermans

                  21.7k439104




                  21.7k439104



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182704%2fhow-to-insert-elements-at-different-locations-of-a-given-list%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay