This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

ReplacePart

ReplacePart
yields an expression in which the ^(th) part of expr is replaced by new.
ReplacePart
replaces parts at positions by .
ReplacePart
replaces the part at position .
ReplacePart
replaces parts at positions by .
ReplacePart
replaces 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->Automatic, 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:
In[1]:=
Click for copyable input
Out[1]=
 
Replace parts 2 and 5:
In[1]:=
Click for copyable input
Out[1]=
 
Replace part of an array:
In[1]:=
Click for copyable input
Out[1]=
 
Replace parts whose positions match a pattern:
In[1]:=
Click for copyable input
Out[1]=
 
Replace parts in any expression:
In[1]:=
Click for copyable input
Out[1]=
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:
ReplacePart works on heads:
Replace all heads by List:
Replace all ordinary parts, but not heads:
Also replace heads:
Never replace heads:
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:
New in 2 | Last modified in 6