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 allclose all

Basic Examples  (6)

Replace part 3:

Replace parts 2 and 5:

Replace part {2,1} of an array:

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:

Replace all values corresponding to key "x":

Replace the head of 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)

Heads  (3)

Replace all ordinary parts, but not heads:

Also replace heads:

Never replace 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:

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:

Replacing the head of an association loses keys:

Extract rules from an association:

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_2024_replacepart, author="Wolfram Research", title="{ReplacePart}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/ReplacePart.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_replacepart, organization={Wolfram Research}, title={ReplacePart}, year={2014}, url={https://reference.wolfram.com/language/ref/ReplacePart.html}, note=[Accessed: 22-December-2024 ]}