WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

ReplacePart [expr,inew]

yields an expression in which the i^(th) 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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Options  
Heads  
Applications  
Properties & Relations  
Possible Issues  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page

ReplacePart

ReplacePart [expr,inew]

yields an expression in which the i^(th) 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 all

Basic Examples  (6)

Replace part 3:

Wolfram Language code: ReplacePart[{a, b, c, d, e}, 3 -> xxx]

Replace parts 2 and 5:

Wolfram Language code: ReplacePart[{a, b, c, d, e}, {2 -> xx, 5 -> yy}]

Replace part {2,1} of an array:

Wolfram Language code: ReplacePart[{{a, b}, {c, d}}, {2, 1} -> xx]

Replace parts whose positions match a pattern:

Wolfram Language code: ReplacePart[{{a, b}, {c, d}}, {i_, i_} -> xx]

Replace parts in any expression:

Wolfram Language code: ReplacePart[a + b + c ^ n, {{3, 2} -> x + y, 2 -> b ^ 100}]

Use a ReplacePart operator:

Wolfram Language code: ReplacePart[3 -> xxx][{a, b, c, d, e}]

Scope  (11)

Replace a part 3 from the end:

Wolfram Language code: ReplacePart[{a, b, c, d, e, f, g}, -3 -> xxx]

Replace several parts by the same expression:

Wolfram Language code: ReplacePart[{a, b, c, d, e, f, g}, {{1}, {3}, {5}} -> xxx]

Part specifications can be patterns:

Wolfram Language code: ReplacePart[{a, b, c, d, e, f, g}, (1 | 3 | 5) -> xxx]

Replace every part except those with indices 1, 3, or 5:

Wolfram Language code: ReplacePart[{a, b, c, d, e, f, g}, Except[1 | 3 | 5] -> xxx]

Replace every part whose index is even:

Wolfram Language code: ReplacePart[{a, b, c, d, e, f, g}, _ ? EvenQ -> xxx]

Replace all elements in the first sublist:

Wolfram Language code: ReplacePart[{{a, b, c}, {d, e}, {f}}, {1, _} -> xx]

Replace the last element in each sublist:

Wolfram Language code: ReplacePart[{{a, b, c}, {d, e}, {f}}, {_, -1} -> xx]

Replace elements on the diagonal:

Wolfram Language code: 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:

Wolfram Language code: ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} :> f[i]]

Patterns can represent part lists of variable length:

Wolfram Language code: ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {___, 2, ___} -> x]
Wolfram Language code: ReplacePart[{{a, b, c}, {d, e}, {f}}, i__ :> s[i]]

Replace a part in an association:

Wolfram Language code: ReplacePart[{<|"x" -> 1, "y" -> 2|>}, {1, 1} -> g]

Replace all values corresponding to key "x":

Wolfram Language code: ReplacePart[{<|"x" -> 1, "y" -> 2|>, <|"x" -> 3, "y" -> 4|>}, {_, "x"} -> f]

Replace the head of an association:

Wolfram Language code: ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> f]

Generalizations & Extensions  (4)

The right-hand side of the rule is evaluated separately for each replacement done:

Wolfram Language code: ReplacePart[{{a, b}, {c, d}}, {i_, i_} :> RandomReal[]]

ReplacePart works with SparseArray objects:

Wolfram Language code: ReplacePart[SparseArray[5 -> a, 10], 7 -> b]
Wolfram Language code: Normal[%]

ReplacePart works on heads:

Wolfram Language code: ReplacePart[f[x, y], 0 -> g]
Wolfram Language code: ReplacePart[f[g][x, y], {0, 1} -> hh]

Replace all heads by List :

Wolfram Language code: ReplacePart[a x ^ 2 + y ^ 2 + c z ^ 2, {___, 0} -> List]

Options  (3)

Heads  (3)

Replace all ordinary parts, but not heads:

Wolfram Language code: ReplacePart[f[x, y], _ -> g]

Also replace heads:

Wolfram Language code: ReplacePart[f[x, y], _ -> g, Heads -> True]

Never replace heads:

Wolfram Language code: ReplacePart[f[x, y], 0 -> g, Heads -> False]

Applications  (8)

Border a matrix with x's:

Wolfram Language code: ReplacePart[IdentityMatrix[5], {_, 1 | 5} -> x]//MatrixForm

Highlight two squares in an array:

Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: ArrayPlot[NestList[ReplacePart[#, RandomInteger[{1, 30}] -> 1]&, Table[0, {30}], 20], Mesh -> All]

Successively replace parts of a list:

Wolfram Language code: FoldList[ReplacePart[#1, #2 -> x]&, {a, b, c, d, e}, {5, 2, 3, 1, 4}]

Successively replace disks in a graphic by circles:

Wolfram Language code: g = Graphics[{Gray, Table[Disk[RandomReal[5, 2]], {5}]}, ImageSize -> Tiny]
Wolfram Language code: Position[g, Disk]
Wolfram Language code: FoldList[ReplacePart[#1, #2 -> Circle]&, g, %]

Successively replace entries in a 2D array:

Wolfram Language code: 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:

Wolfram Language code: ReplacePart[ConstantArray[0, {5, 5}], {x_, y_} /; CoprimeQ[x, y] :> x + y]

Properties & Relations  (5)

ReplacePart uses rules in the order given:

Wolfram Language code: ReplacePart[{a, b, c, d, e}, {3 -> u, _ -> x}]

ReplacePart takes lists of positions in the same form as generated by Position :

Wolfram Language code: Position[{a, b, x, c, d, x, e}, x]
Wolfram Language code: ReplacePart[{a, b, x, c, d, x, e}, {{3}, {6}} -> yy]

ReplacePart takes the same part rules as SparseArray :

Wolfram Language code: SparseArray[{1 -> x, 5 -> y}, 10]//Normal
Wolfram Language code: ReplacePart[Array[0&, 10], {1 -> x, 5 -> y}]

ReplacePart rewrites subexpressions at a particular position:

Wolfram Language code: ReplacePart[f[1, x, 3], {2} -> 0]

Replace rewrites parts that match a pattern at a particular level:

Wolfram Language code: Replace[f[1, x, 3], Except[_ ? NumericQ] :> 0, {1}]

ReplacePart replaces parts of expressions whose positions match a pattern:

Wolfram Language code: ReplacePart[{5, 4, 3, 2, 1} , _ ? PrimeQ -> "p"]

ReplaceAll replaces parts of expressions that themselves match a pattern:

Wolfram Language code: {5, 4, 3, 2, 1} /. _ ? PrimeQ -> "p"

Possible Issues  (4)

ReplacePart only affects parts that are already present:

Wolfram Language code: ReplacePart[{a, b, c, d}, 5 -> x]

Particularly in an Orderless function, the order of parts may change when they are replaced:

Wolfram Language code: ReplacePart[ReplacePart[a + b + c, 1 -> x], 3 -> y]

Replacing an empty list of positions does not change the expression:

Wolfram Language code: ReplacePart[h[a, b], {} -> x]

Position {} corresponds to the whole expression:

Wolfram Language code: ReplacePart[h[a, b], {{}} -> x]

Replacing the head of an association loses keys:

Wolfram Language code: ReplacePart[<|"x" -> 1, "y" -> 2|>, {0} -> List]

Extract rules from an association:

Wolfram Language code: Normal[<|"x" -> 1, "y" -> 2|>]

History

Introduced in 1991 (2.0) | Updated in 1996 (3.0) 2003 (5.0) 2007 (6.0) 2014 (10.0)

Wolfram Research (1991), ReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplacePart.html (updated 2014).

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: 14-August-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: 14-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /