ReplaceAt
✖
ReplaceAt
Details

- ReplaceAt is typically used to change one or more elements of an expression, based on supplied replacement rules.
- ReplaceAt[expr,rules,{i,j,…}] replaces the part expr[[i,j,…]] using rules.
- ReplaceAt[expr,rules,{{i1,j1,…},{i2,j2,…},…}] replaces the parts expr[[i1,j1,…]], expr[[i2,j2,…]], … using rules.
- The rules must be of the form lhs->rhs or lhs:>rhs, or a list of such rules.
- A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned. If none of the rules apply, the original expr is returned.
- ReplaceAt[expr,patt:>new,pos] can be used to replace a part without first evaluating it. With a rule such as pattnew, new is evaluated separately for each expression that matches patt.
- ReplaceAt applies replacement rules repeatedly to a particular part if that part is mentioned more than once in the list of positions.
- The list of positions used by ReplaceAt is in the same form as is returned by the function Position.
- ReplaceAt works on Association objects, using the same specification for keys as in Part.
- If expr is a SparseArray object or structured array, ReplaceAt[expr,…] replaces parts in the corresponding ordinary array.
- ReplaceAt[rules,pos][expr] is equivalent to ReplaceAt[expr,rules,pos].
Examples
open allclose allBasic Examples (9)Summary of the most common use cases
Replace a specified part of an expression using a rule:

https://wolfram.com/xid/0bdpqbj1k-ks0bns


https://wolfram.com/xid/0bdpqbj1k-neqqel


https://wolfram.com/xid/0bdpqbj1k-kj9jwr

Use negative positions to count from the end:

https://wolfram.com/xid/0bdpqbj1k-fy0y63


https://wolfram.com/xid/0bdpqbj1k-phyfi1

Use pattern-based replacements:

https://wolfram.com/xid/0bdpqbj1k-kmz0zs

Use multiple replacement rules:

https://wolfram.com/xid/0bdpqbj1k-edxmuc

Replace the second element of all top-level parts (the “second column”):

https://wolfram.com/xid/0bdpqbj1k-kdu5vi

Replace parts of an association specified by Key:

https://wolfram.com/xid/0bdpqbj1k-n44xg1


https://wolfram.com/xid/0bdpqbj1k-c247vp

Use the operator form of ReplaceAt:

https://wolfram.com/xid/0bdpqbj1k-ix2au6

Scope (6)Survey of the scope of standard use cases
A single element of the expression:

https://wolfram.com/xid/0bdpqbj1k-dzjfkj

Negative numbers count from the end:

https://wolfram.com/xid/0bdpqbj1k-qqersb


https://wolfram.com/xid/0bdpqbj1k-gm35ye


https://wolfram.com/xid/0bdpqbj1k-ovc7ve


https://wolfram.com/xid/0bdpqbj1k-ct6zfg

Use Span syntax:

https://wolfram.com/xid/0bdpqbj1k-om4yfw

Generalizations & Extensions (4)Generalized and extended use cases
ReplaceAt can be used on expressions with any head:

https://wolfram.com/xid/0bdpqbj1k-gnffva


https://wolfram.com/xid/0bdpqbj1k-guxgmv

Key and integer part specifications in associations are different:

https://wolfram.com/xid/0bdpqbj1k-dahzrw

Use part 0 to perform replacements in the head of an expression:

https://wolfram.com/xid/0bdpqbj1k-lhwza

ReplaceAt can be used with sparse arrays:

https://wolfram.com/xid/0bdpqbj1k-lftjy1


https://wolfram.com/xid/0bdpqbj1k-gwzo3v

Properties & Relations (7)Properties of the function, and connections to other functions
ReplaceAt can use lists of parts returned by Position:

https://wolfram.com/xid/0bdpqbj1k-qk7d71


https://wolfram.com/xid/0bdpqbj1k-op3n7j


https://wolfram.com/xid/0bdpqbj1k-ekeeo1

These are the subexpressions returned by Extract:

https://wolfram.com/xid/0bdpqbj1k-qrvb5r

ReplaceAt can use an Association for replacement rules:

https://wolfram.com/xid/0bdpqbj1k-ocuhdb


https://wolfram.com/xid/0bdpqbj1k-qaffcq

ReplaceAt can use a Dispatch object for replacement rules:

https://wolfram.com/xid/0bdpqbj1k-ebvdjm


https://wolfram.com/xid/0bdpqbj1k-gvoxso

ReplaceAt applies replacement rules repeatedly if a part is mentioned repeatedly:

https://wolfram.com/xid/0bdpqbj1k-m9j2bx

ReplaceAt will not evaluate held replacements:

https://wolfram.com/xid/0bdpqbj1k-er5wdp

Manipulate code in an unevaluated association:

https://wolfram.com/xid/0bdpqbj1k-yyo4r


https://wolfram.com/xid/0bdpqbj1k-ei4kd


https://wolfram.com/xid/0bdpqbj1k-qe3obz


Use Unevaluated to perform replacements prior to evaluating expr:

https://wolfram.com/xid/0bdpqbj1k-gsg3ya

Possible Issues (1)Common pitfalls and unexpected behavior
Repeated replacement parts may produce unexpected results if the structure of the expression is expected to change during replacements:

https://wolfram.com/xid/0bdpqbj1k-djzrtn

Compare to using Nest:

https://wolfram.com/xid/0bdpqbj1k-h1ldkc

This behavior is consistent with ReplacePart and MapAt:

https://wolfram.com/xid/0bdpqbj1k-oy7w6i


https://wolfram.com/xid/0bdpqbj1k-l3telm

Wolfram Research (2022), ReplaceAt, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAt.html.
Text
Wolfram Research (2022), ReplaceAt, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAt.html.
Wolfram Research (2022), ReplaceAt, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAt.html.
CMS
Wolfram Language. 2022. "ReplaceAt." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ReplaceAt.html.
Wolfram Language. 2022. "ReplaceAt." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ReplaceAt.html.
APA
Wolfram Language. (2022). ReplaceAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceAt.html
Wolfram Language. (2022). ReplaceAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceAt.html
BibTeX
@misc{reference.wolfram_2025_replaceat, author="Wolfram Research", title="{ReplaceAt}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/ReplaceAt.html}", note=[Accessed: 09-July-2025
]}
BibLaTeX
@online{reference.wolfram_2025_replaceat, organization={Wolfram Research}, title={ReplaceAt}, year={2022}, url={https://reference.wolfram.com/language/ref/ReplaceAt.html}, note=[Accessed: 09-July-2025
]}