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
4
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.♦
    3 hours ago










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    1 hour ago










  • @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.♦
    1 hour ago














up vote
4
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.♦
    3 hours ago










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    1 hour ago










  • @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.♦
    1 hour ago












up vote
4
down vote

favorite









up vote
4
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 1 hour ago









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

2,204725




2,204725










asked 3 hours ago









Soumyajit Roy

1358




1358







  • 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.♦
    3 hours ago










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    1 hour ago










  • @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.♦
    1 hour ago












  • 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.♦
    3 hours ago










  • @J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
    – Henrik Schumacher
    1 hour ago










  • @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.♦
    1 hour ago







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.♦
3 hours ago




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












@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
– Henrik Schumacher
1 hour ago




@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
– Henrik Schumacher
1 hour ago












@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.♦
1 hour ago




@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.♦
1 hour ago










3 Answers
3






active

oldest

votes

















up vote
4
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
    1
    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




















    • Anyway, wow! (IOU one upvote.)
      – J. M. is somewhat okay.♦
      1 hour ago











    • I wonder if this package has similar functions.
      – Î‘λέξανδρος Ζεγγ
      46 mins ago











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


















    up vote
    0
    down vote













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


    Mathematica graphics





    share




















      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
      4
      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
        4
        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
          4
          down vote










          up vote
          4
          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 2 hours ago

























          answered 2 hours ago









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

          2,204725




          2,204725




















              up vote
              1
              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




















              • Anyway, wow! (IOU one upvote.)
                – J. M. is somewhat okay.♦
                1 hour ago











              • I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                46 mins ago











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















              up vote
              1
              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




















              • Anyway, wow! (IOU one upvote.)
                – J. M. is somewhat okay.♦
                1 hour ago











              • I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                46 mins ago











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













              up vote
              1
              down vote










              up vote
              1
              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 1 hour ago









              Henrik Schumacher

              39.6k254118




              39.6k254118











              • Anyway, wow! (IOU one upvote.)
                – J. M. is somewhat okay.♦
                1 hour ago











              • I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                46 mins ago











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

















              • Anyway, wow! (IOU one upvote.)
                – J. M. is somewhat okay.♦
                1 hour ago











              • I wonder if this package has similar functions.
                – Î‘λέξανδρος Ζεγγ
                46 mins ago











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
















              Anyway, wow! (IOU one upvote.)
              – J. M. is somewhat okay.♦
              1 hour ago





              Anyway, wow! (IOU one upvote.)
              – J. M. is somewhat okay.♦
              1 hour ago













              I wonder if this package has similar functions.
              – Î‘λέξανδρος Ζεγγ
              46 mins ago





              I wonder if this package has similar functions.
              – Î‘λέξανδρος Ζεγγ
              46 mins ago













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





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











              up vote
              0
              down vote













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


              Mathematica graphics





              share
























                up vote
                0
                down vote













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


                Mathematica graphics





                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









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


                  Mathematica graphics





                  share












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


                  Mathematica graphics






                  share











                  share


                  share










                  answered 7 mins ago









                  rhermans

                  21.6k439104




                  21.6k439104



























                       

                      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













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      What does second last employer means? [closed]

                      One-line joke