This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Lists and Matrices / List Operations  /
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 i , j , ... in expr.
  • Insert[ expr , elem , , , ... , , , ... , ... ] inserts elem at several positions.
  • Examples: Insert[ a, b, c , x, 2].
  • Insert[ a, b, c , x, 1 , -1 ].
  • Insert[ a, b , c, d , x, 2, 1 ].
  • list can have any head, not necessarily List.
  • See the Mathematica book: Section 1.8.6.
  • See also: Prepend, Append, StringInsert, Take, Drop, Delete, ReplacePart, FlattenAt, Position, Sequence.

    Further Examples

    This inserts d into the fourth position of this list.

    In[1]:=

    Out[1]=

    This inserts b in the first and second positions of this list.

    In[2]:=

    Out[2]=

    This function builds a random permutation of Range[n] by inserting one element at a time.

    In[3]:=

    In[4]:=

    Out[4]=

    This function "sows" copies of the specified object between each pair of entries of expr.

    In[5]:=

    In[6]:=

    Out[6]=

    This variant also places copies before the first and after the last elements.

    In[7]:=

    In[8]:=

    Out[8]=

    In[9]:=