ReplacePart
ReplacePart[expr,inew]
yields an expression in which the i part of expr is replaced by new.
ReplacePart[expr,{i1new1,i2new2,…}]
replaces parts at positions in by newn.
ReplacePart[expr,{i,j,…}new]
replaces the part at position {i,j,…}.
ReplacePart[expr,{{i1,j1,…}new1,…}]
replaces parts at positions {in,jn,…} by newn.
ReplacePart[expr,{{i1,j1,…},…}new]
replaces all parts at positions {in,jn,…} by new.
ReplacePart[inew]
represents an operator form of ReplacePart that can be applied to an expression.
Details 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 x_) 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 patt:>new, 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 0.
- With Heads->True a head is replaced whenever the corresponding position specification matches 0.
- Heads->False never replaces heads.
- ReplacePart can be used on SparseArray objects.
- ReplacePart[i->new][expr] is equivalent to ReplacePart[expr,i->new].
Examples
open allclose allBasic Examples (6)
Replace parts whose positions match a pattern:
Replace parts in any expression:
Use a ReplacePart operator:
Scope (11)
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:
Replace a part in an association:
Generalizations & Extensions (4)
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:
Options (3)
Applications (8)
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:
Properties & Relations (5)
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 rewrites subexpressions at a particular position:
Replace rewrites parts that match a pattern at a particular level:
ReplacePart replaces parts of expressions whose positions match a pattern:
ReplaceAll replaces parts of expressions that themselves match a pattern:
Possible Issues (4)
ReplacePart only affects parts that are already present:
Particularly in an Orderless function, the order of parts may change when they are replaced:
Replacing an empty list of positions does not change the expression:
Position {} corresponds to the whole expression:
Text
Wolfram Research (1991), ReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplacePart.html (updated 2014).
CMS
Wolfram Language. 1991. "ReplacePart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/ReplacePart.html.
APA
Wolfram Language. (1991). ReplacePart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplacePart.html