I would like to take the following data:
insertvaluesinsertValues = {r, x};
insertpositionsinsertPositions = {{1}, {5}};
origlistoriginalList = {a, b, c, d, e, f, g};
and generate output that looks like this:
{r,a,b,c,d,x,e,f,g}
Mathematica'sMathematica's Insert
command seems limited in that it does not allow you to insert multiple elements simultaneously into different positions in a list.
I wrote an ugly WhileWhile[]
loop to accomplish this, and it works, but seems inappropriate for Mathematica's functional approach to coding. Is there a "simpler" way to accomplish this goal with a functional approach? Thank you.
insertvaluesinsertValues = {r, x};
insertpositionsinsertPositions = {{1}, {5}};
origlistoriginalList = {a, b, c, d, e, f, g};
loopcounterloopCounter = 0; poscounter
posCounter = 0;
final = origlist;originalList;
While[loopcounterWhile[loopCounter < Length[insertvalues]Length[insertValues],
final =
Insert[final,
insertValues[[loopCounter + 1]],
insertvalues[[loopcounter +
1]], (insertpositions[[loopcounterinsertpositions[[loopCounter + 1]] + poscounterposCounter)];
loopcounter++;loopCounter++; poscounter++];
posCounter++
];