Insert

Insert[list,elem,n]
inserts elem at position n in list. If n is negative, the position is counted from the end.

Insert[expr,elem,{i,j,}]
inserts elem at position in expr.

Insert[expr,elem,{{i1,j1,},{i2,j2,},}]
inserts elem at several positions.

Insert[elem,pos]
represents an operator form of Insert that can be applied to an expression.

DetailsDetails

  • list can have any head, not necessarily List.
  • Insert works on SparseArray objects by effectively inserting into the corresponding ordinary lists.
  • If expr is an Association object, Insert[expr,key->value,pos] inserts an element in the association.
  • If the specified position is of the form or Key[k], then Insert inserts a new element before the one corresponding to this key, or overwrites the value if this key is already present.
  • If the key of the element to be inserted is already present, then the old element with the same key is deleted.
  • Insert[elem,pos][expr] is equivalent to Insert[expr,elem,pos].

ExamplesExamplesopen allclose all

Basic Examples  (10)Basic Examples  (10)

Insert to be at position :

In[1]:=
Click for copyable input
Out[1]=

Insert to be at position 2 from the end:

In[1]:=
Click for copyable input
Out[1]=

Insert at several positions:

In[1]:=
Click for copyable input
Out[1]=

Insert into a 2D array:

In[1]:=
Click for copyable input
Out[1]=

Insert an element before a

In[1]:=
Click for copyable input
Out[1]=

Insert several elements:

In[1]:=
Click for copyable input
Out[1]=

Inserting entries with repeating keys in an Association overrides earlier values:

In[1]:=
Click for copyable input
Out[1]=

Insert in an Association using position:

In[1]:=
Click for copyable input
Out[1]=

Insert after the last element:

In[1]:=
Click for copyable input
Out[1]=

Insert at deeper levels:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 1988
(1.0)
| Updated in 2014
(10.0)