This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
Wolfram Research, Inc.

Testing and Searching List ElementsCombining Lists

1.8.6 Adding, Removing and Modifying List Elements

Functions for adding and deleting elements in lists.

This gives a list with x prepended.

In[1]:= Prepend[{a, b, c}, x]


This adds x at the end.

In[2]:= Append[{a, b, c}, x]


This inserts x so that it becomes element number 2.

In[3]:= Insert[{a, b, c}, x, 2]


Negative numbers count from the end of the list.

In[4]:= Insert[{a, b, c}, x, -2]


Delete removes an element from the list.

In[5]:= Delete[{a, b, c, d}, 3]


Modifying parts of lists.

This replaces the third element in the list with x.

In[6]:= ReplacePart[{a, b, c, d}, x, 3]


This replaces the first and fourth parts of the list. Notice the need for double lists in specifying multiple parts to replace.

In[7]:= ReplacePart[{a, b, c, d}, x, {{1}, {4}}]


Here is a identity matrix.

In[8]:= IdentityMatrix[3]


This replaces the component of the matrix by x.

In[9]:= ReplacePart[%, x, {2, 2}]


Padding lists.

This pads with zeros on the left to make a list of length 10.

In[10]:= PadLeft[{a, b, c}, 10]


This pads with copies of the sequence x, y.

In[11]:= PadLeft[{a, b, c}, 10, {x, y}]


This leaves a margin of 3 elements on the right.

In[12]:= PadLeft[{a, b, c}, 10, {x, y}, 3]


Testing and Searching List ElementsCombining Lists