This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.

# ReplacePart

 ReplacePartyields an expression in which the part of expr is replaced by new. ReplacePart replaces parts at positions by . ReplacePartreplaces the part at position . ReplacePartreplaces parts at positions by . ReplacePartreplaces all parts at positions by new.
• 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 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 Heads, a head is replaced only when the corresponding position specification is explicitly .
• With Heads->True a head is replaced whenever the corresponding position specification matches .
Replace part 3:
Replace parts 2 and 5:
Replace part of an array:
Replace parts whose positions match a pattern:
Replace parts in any expression:
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]=
 Scope   (8)
Replace a part 3 from the end:
Replace several parts by the same expression:
Part specifications can be patterns:
Replace every part except those with indices 1, 3, or 5:
Replace every part whose index is even:
Replace all elements in the first sublist:
Replace the last element in each sublist:
Replace elements on the diagonal:
Part specification patterns can contain variables that are used in the replacements:
Patterns can represent part lists of variable length:
The right-hand side of the rule is evaluated separately for each replacement done:
ReplacePart works with SparseArray objects:
 Options   (3)
Replace all ordinary parts, but not heads:
 Applications   (8)
Border a matrix with x's:
Highlight two squares in an array:
Generate a difference pattern for two cellular automaton initial conditions differing by one bit:
Insert a black cell at a random position at each step:
Successively replace parts of a list:
Successively replace disks in a graphic by circles:
Successively replace entries in a 2D array:
Replace elements whose indices are not relatively prime:
ReplacePart uses rules in the order given:
ReplacePart takes lists of positions in the same form as generated by Position:
ReplacePart takes the same part rules as SparseArray:
ReplacePart only affects parts that are already present:
Particularly in an Orderless function, the order of parts may change when they are replaced: