WolframLang: Add Element to List
Functions that operate on list usually do not modify the list, nor a variable holding the list. They return a new list. This is true for vast majority of WolframLang functions.
If you want to modify the variable that hold a list, you assign it back, e.g.
list = f[ list, etc ]
Function whose name end in βToβ, e.g. AppendTo
modifies the variable. (the argument must be a variable.)
Insert
Insert[list, elem, n]
-
Insert an element at Position
{n}
same asInsert[list, elem, {n}]
Insert[ {3, 4}, x, 2 ] === {3, x, 4}
Insert[list, elem, {i, j, k}]
-
- Insert an element at Position
{i, j, k}
- Return a new list. (Does not modify the list.)
Insert[ {3, 4}, x, {2} ] === {3, x, 4} Insert[ {3, {4}}, x, {2,1} ] === {3, {x, 4}}
- Insert an element at Position
Insert[list, elem, {pos1, pos2 etc }]
-
insert an element at several positions
Insert[ {{1,0}, {2,0}, {3,0}}, x, {{2}, {3,1}} ] === {{1, 0}, x, {2, 0}, {x, 3, 0}}
Append, Prepend
Append[expr, new]
-
return a new expr with new item added at end.
Append
Append[ List[ 3, 4 ], 5 ] === {3, 4, 5}
AppendTo[x,elem]
-
Same as
x=Append[x,elem]
AppendTo Prepend
-
Add to beginning
Prepend
Prepend[ List[ 3, 4 ], 5 ] === { 5, 3, 4}
PrependTo[x,elem]
-
Same as
x=Prepend[x,elem]
PrependTo