BUILT-IN MATHEMATICA SYMBOL

# ReplacePart

ReplacePart[expr, i->new]
yields an expression in which the part of expr is replaced by new.

ReplacePart[expr, {i1->new1, i2->new2, ...}]
replaces parts at positions by .

ReplacePart[expr, {i, j, ...}->new]
replaces the part at position .

ReplacePart[expr, {{i1, j1, ...}->new1, ...}]
replaces parts at positions by .

ReplacePart[expr, {{i1, j1, ...}, ...}->new]
replaces all parts at positions by new.

## Details and OptionsDetails and Options

• Explicit negative part numbers count from the end. »
• Part position specifications can be patterns.
• Each pattern is effectively tested against each list of part numbers for parts in expr.
• Patterns p (such as ) that are not explicitly lists are treated as {p}.
• Patterns can include constructs such as , representing position specifications of variable lengths.
• ReplacePart[expr, i:>new] can be used to replace a part without first evaluating it. With a rule such as , new is evaluated separately for each position that matches patt.
• With the default option setting , a head is replaced only when the corresponding position specification is explicitly .
• With a head is replaced whenever the corresponding position specification matches .
• ReplacePart can be used on SparseArray objects.

## ExamplesExamplesopen allclose all

### Basic Examples (5)Basic Examples (5)

Replace part 3:

 Out[1]=

Replace parts 2 and 5:

 Out[1]=

Replace part of an array:

 Out[1]=

Replace parts whose positions match a pattern:

 Out[1]=

Replace parts in any expression:

 Out[1]=