WolframLang: Add Element to List

By Xah Lee. Date: . Last updated: .

Note, functions that operate on list usually do not modify a variable holding a list. They return a new list. This is true for vast majority of WolframLang functions.

So, if you want to modify a variable, you assign it back, e.g. xlist = Insert[ xlist, ... ]

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 as Insert[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
Insert[ {3, 4}, x, {2} ] === {3, x, 4}
Insert[ {3, {4}}, x, {2,1} ] === {3, {x, 4}}
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

List

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc