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.
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 all close allBasic Examples (6)
ReplacePart[{a, b, c, d, e}, 3 -> xxx]ReplacePart[{a, b, c, d, e}, {2 -> xx, 5 -> yy}]ReplacePart[{{a, b}, {c, d}}, {2, 1} -> xx]Replace parts whose positions match a pattern:
ReplacePart[{{a, b}, {c, d}}, {i_, i_} -> xx]Replace parts in any expression:
ReplacePart[a + b + c ^ n, {{3, 2} -> x + y, 2 -> b ^ 100}]Use a ReplacePart operator:
ReplacePart[3 -> xxx][{a, b, c, d, e}]Scope (11)
Replace a part 3 from the end:
ReplacePart[{a, b, c, d, e, f, g}, -3 -> xxx]Replace several parts by the same expression:
ReplacePart[{a, b, c, d, e, f, g}, {{1}, {3}, {5}} -> xxx]Part specifications can be patterns:
ReplacePart[{a, b, c, d, e, f, g}, (1 | 3 | 5) -> xxx]Replace every part except those with indices 1, 3, or 5:
ReplacePart[{a, b, c, d, e, f, g}, Except[1 | 3 | 5] -> xxx]Replace every part whose index is even:
ReplacePart[{a, b, c, d, e, f, g}, _ ? EvenQ -> xxx]Replace all elements in the first sublist:
ReplacePart[{{a, b, c}, {d, e}, {f}}, {1, _} -> xx]Replace the last element in each sublist:
ReplacePart[{{a, b, c}, {d, e}, {f}}, {_, -1} -> xx]Replace elements on the diagonal:
ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} -> x]Part specification patterns can contain variables that are used in the replacements:
ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} :> f[i]]Patterns can represent part lists of variable length:
ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {___, 2, ___} -> x]ReplacePart[{{a, b, c}, {d, e}, {f}}, i__ :> s[i]]Replace a part in an association:
ReplacePart[{<|"x" -> 1, "y" -> 2|>}, {1, 1} -> g]Replace all values corresponding to key "x":
ReplacePart[{<|"x" -> 1, "y" -> 2|>, <|"x" -> 3, "y" -> 4|>}, {_, "x"} -> f]Replace the head of an association:
ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> f]Generalizations & Extensions (4)
The right-hand side of the rule is evaluated separately for each replacement done:
ReplacePart[{{a, b}, {c, d}}, {i_, i_} :> RandomReal[]]ReplacePart works with SparseArray objects:
ReplacePart[SparseArray[5 -> a, 10], 7 -> b]Normal[%]ReplacePart works on heads:
ReplacePart[f[x, y], 0 -> g]ReplacePart[f[g][x, y], {0, 1} -> hh]Replace all heads by List:
ReplacePart[a x ^ 2 + y ^ 2 + c z ^ 2, {___, 0} -> List]Options (3)
Applications (8)
ReplacePart[IdentityMatrix[5], {_, 1 | 5} -> x]//MatrixFormHighlight two squares in an array:
ArrayPlot[ReplacePart[Array[GCD, {15, 15}], {{6, 6}, {12, 12}} -> Red]]Generate a difference pattern for two cellular automaton initial conditions differing by one bit:
With[{u = RandomInteger[1, 100]}, ArrayPlot[Sum[(-1) ^ i CellularAutomaton[30, ReplacePart[u, 50 -> i], 50], {i, 0, 1}]]]Insert a black cell at a random position at each step:
ArrayPlot[NestList[ReplacePart[#, RandomInteger[{1, 30}] -> 1]&, Table[0, {30}], 20], Mesh -> All]Successively replace parts of a list:
FoldList[ReplacePart[#1, #2 -> x]&, {a, b, c, d, e}, {5, 2, 3, 1, 4}]Successively replace disks in a graphic by circles:
g = Graphics[{Gray, Table[Disk[RandomReal[5, 2]], {5}]}, ImageSize -> Tiny]Position[g, Disk]FoldList[ReplacePart[#1, #2 -> Circle]&, g, %]Successively replace entries in a 2D array:
ArrayPlot[#, Mesh -> True, ImageSize -> 50]& /@ NestList[ReplacePart[#, RandomInteger[{1, 5}, {2}] -> 1]&, ConstantArray[0, {5, 5}], 10]Replace elements whose indices are not relatively prime:
ReplacePart[ConstantArray[0, {5, 5}], {x_, y_} /; CoprimeQ[x, y] :> x + y]Properties & Relations (5)
ReplacePart uses rules in the order given:
ReplacePart[{a, b, c, d, e}, {3 -> u, _ -> x}]ReplacePart takes lists of positions in the same form as generated by Position:
Position[{a, b, x, c, d, x, e}, x]ReplacePart[{a, b, x, c, d, x, e}, {{3}, {6}} -> yy]ReplacePart takes the same part rules as SparseArray:
SparseArray[{1 -> x, 5 -> y}, 10]//NormalReplacePart[Array[0&, 10], {1 -> x, 5 -> y}]ReplacePart rewrites subexpressions at a particular position:
ReplacePart[f[1, x, 3], {2} -> 0]Replace rewrites parts that match a pattern at a particular level:
Replace[f[1, x, 3], Except[_ ? NumericQ] :> 0, {1}]ReplacePart replaces parts of expressions whose positions match a pattern:
ReplacePart[{5, 4, 3, 2, 1} , _ ? PrimeQ -> "p"]ReplaceAll replaces parts of expressions that themselves match a pattern:
{5, 4, 3, 2, 1} /. _ ? PrimeQ -> "p"Possible Issues (4)
ReplacePart only affects parts that are already present:
ReplacePart[{a, b, c, d}, 5 -> x]Particularly in an Orderless function, the order of parts may change when they are replaced:
ReplacePart[ReplacePart[a + b + c, 1 -> x], 3 -> y]Replacing an empty list of positions does not change the expression:
ReplacePart[h[a, b], {} -> x]Position {} corresponds to the whole expression:
ReplacePart[h[a, b], {{}} -> x]Replacing the head of an association loses keys:
ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> List]Extract rules from an association:
Normal[<|"x" -> 1, "y" -> 2|>]See Also
Part Set Extract ReplaceAt SubsetMap MapAt FlattenAt SparseArray Insert Delete Nothing Sequence StringReplacePart BitSet NetReplacePart
Function Repository: ReplaceAt ReplacePartWith
History
Introduced in 1991 (2.0) | Updated in 1996 (3.0) ▪ 2003 (5.0) ▪ 2007 (6.0) ▪ 2014 (10.0)
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
BibTeX
@misc{reference.wolfram_2026_replacepart, author="Wolfram Research", title="{ReplacePart}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/ReplacePart.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_replacepart, organization={Wolfram Research}, title={ReplacePart}, year={2014}, url={https://reference.wolfram.com/language/ref/ReplacePart.html}, note=[Accessed: 12-June-2026]}