Function Repository Resource:

ReplaceAt

Source Notebook

Replace parts of an expression using replacement rules and a part specification

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ReplaceAt"][expr,rules,n]

transforms expr by replacing the nth element using rules.

ResourceFunction["ReplaceAt"][expr,rules,{i,j,}]

replaces the part of expr at position {i,j,}.

ResourceFunction["ReplaceAt"][expr,rules,{{i1,j1,},{i2,j2,},}]

replaces parts at several positions.

ResourceFunction["ReplaceAt"][rules,pos]

represents an operator form of ResourceFunction["ReplaceAt"] that can be applied to an expression.

Details and Options

The rules must be of the form lhsrhs or lhsrhs.
A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned and is used for a particular replacement.
Multiple rules can also be given as an Association or as a Dispatch.
The list of positions used by ResourceFunction["ReplaceAt"] is in the same form as is returned by the function Position.
ResourceFunction["ReplaceAt"][expr,inew,pos] can be used to replace a part without first evaluating it. With a rule such as pattnew, new is evaluated separately for each position that matches patt.
Delayed rules defined with can contain /; conditions.
ResourceFunction["ReplaceAt"] works on Association objects, using the same specification for keys as in Part.
ResourceFunction["ReplaceAt"] can be used on SparseArray objects.
ResourceFunction["ReplaceAt"] applies replacement rules repeatedly to a particular part if that part is mentioned more than once in the list of positions.
ResourceFunction["ReplaceAt"][rules,pos][expr] is effectively equivalent to ResourceFunction["ReplaceAt"][expr,rules,pos].

Examples

Basic Examples (9) 

Replace a specified part of an expression using a rule:

In[1]:=
ResourceFunction["ReplaceAt"][{a, a, a, a}, a -> xx, 2]
Out[1]=

Replace multiple parts:

In[2]:=
ResourceFunction["ReplaceAt"][{a, a, a, a}, a -> xx, {{1}, {4}}]
Out[2]=

Replace a nested part:

In[3]:=
ResourceFunction["ReplaceAt"][{{a, a}, {a, a}}, a -> xx, {2, 1}]
Out[3]=

Use negative positions to count from the end:

In[4]:=
ResourceFunction["ReplaceAt"][{a, a, a, a}, a -> xx, -2]
Out[4]=
In[5]:=
ResourceFunction["ReplaceAt"][{{a, a, a}, {a, a, a}}, a -> xx, {-1, -2}]
Out[5]=

Use pattern-based replacements:

In[6]:=
ResourceFunction["ReplaceAt"][{1, 2, 3, 4}, x_ :> 2 x - 1, {{2}, {4}}]
Out[6]=

Use multiple replacement rules:

In[7]:=
ResourceFunction[
 "ReplaceAt"][{a, b, c, d}, {a -> xx, _ -> yy}, {{1}, {2}, {4}}]
Out[7]=

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

In[8]:=
ResourceFunction["ReplaceAt"][{{a, a}, {a, a}}, a -> xx, {All, 2}]
Out[8]=

Replace parts of an association specified by Key:

In[9]:=
ResourceFunction["ReplaceAt"][<|"a" -> a, "b" -> a|>, a -> xx, Key["a"]]
Out[9]=
In[10]:=
ResourceFunction["ReplaceAt"][<|"a" -> {a, a}, "b" -> {a, a}|>, a -> xx, {Key["a"], All}]
Out[10]=

Use the operator form of ReplaceAt:

In[11]:=
ResourceFunction["ReplaceAt"][a -> xx, 2][{a, a, a, a}]
Out[11]=

Scope (6) 

Part Specifications (6) 

A single element of the expression:

In[12]:=
ResourceFunction["ReplaceAt"][{{a, b}, {c, d}, e}, x_ :> f[x], 2]
Out[12]=

Negative numbers count from the end:

In[13]:=
ResourceFunction["ReplaceAt"][{{a, b}, {c, d}, e}, x_ :> f[x], -1]
Out[13]=

Replace parts at level 2:

In[14]:=
ResourceFunction["ReplaceAt"][{{a, b}, {c, d}, e}, x_ :> f[x], {2, 1}]
Out[14]=

Replace several parts:

In[15]:=
ResourceFunction["ReplaceAt"][{{a, b}, {c, d}, e}, x_ :> f[x], {{1}, {3}}]
Out[15]=

Replace several nested parts:

In[16]:=
ResourceFunction["ReplaceAt"][{{a, b}, {c, d}, e}, x_ :> f[x], {{1, 2}, {2, 2}, {3}}]
Out[16]=

Use Span syntax:

In[17]:=
ResourceFunction["ReplaceAt"][{a, a, a, a, a}, a -> xx, 2 ;; 4]
Out[17]=

Generalizations and Extensions (4) 

ReplaceAt can be used on expressions with any head:

In[18]:=
ResourceFunction["ReplaceAt"][a + b + c + d, _ -> x, 2]
Out[18]=
In[19]:=
ResourceFunction["ReplaceAt"][x^2 + y^2, _ -> z, {{1, 1}, {2, 1}}]
Out[19]=

Key and integer part specifications in associations are different:

In[20]:=
ResourceFunction[
 "ReplaceAt"][<|3 -> "a", 2 -> "b", 1 -> "c"|>, _ -> xx , {{1}, {Key[1]}}]
Out[20]=

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

In[21]:=
ResourceFunction["ReplaceAt"][{a, b, c}, _ -> f, 0]
Out[21]=

ReplaceAt can be used with sparse arrays:

In[22]:=
arr = SparseArray[{1 -> 1, 2 -> 2, 10 -> 10}]
Out[22]=
In[23]:=
ResourceFunction["ReplaceAt"][arr, _ -> xx, {{2}, {3}}]
Out[23]=

Properties and Relations (7) 

ReplaceAt can use lists of parts returned by Position:

In[24]:=
t = {x, x^2, y, y^2, z^3}
Out[24]=
In[25]:=
pos = Position[t, _^_]
Out[25]=
In[26]:=
ResourceFunction["ReplaceAt"][t, x_ :> x + 1, pos]
Out[26]=

These are the subexpressions returned by Extract:

In[27]:=
Extract[t, pos]
Out[27]=

ReplaceAt can use an Association for replacement rules:

In[28]:=
assoc = <|a -> b, b -> c, c -> a, d -> e, e -> d|>
Out[28]=
In[29]:=
ResourceFunction["ReplaceAt"][{a, b, c, d, e}, assoc, All]
Out[29]=

ReplaceAt can use a Dispatch object for replacement rules:

In[30]:=
dispatch = Dispatch[{a -> b, b -> c, c -> a, d -> e, e -> d}]
Out[30]=
In[31]:=
ResourceFunction["ReplaceAt"][{a, b, c, d, e}, dispatch, All]
Out[31]=

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

In[32]:=
ResourceFunction["ReplaceAt"][{a, b, c, d}, x_ :> f[x], {{1}, {3}, {1}}]
Out[32]=

ReplaceAt will not evaluate held replacements:

In[33]:=
ResourceFunction["ReplaceAt"][Hold[1, 2, 3], x_ :> Echo[x + 1], 2]
Out[33]=

Manipulate code in an unevaluated association:

In[34]:=
assoc = ResourceFunction["UnevaluatedAssociation"][1 + 1 -> Print[2], 2 + 2 -> Print[4]]
Out[34]=
In[35]:=
ResourceFunction["ReplaceAt"][assoc, Print -> Echo, {All, 0}]
Out[35]=

Evaluate upon retrieval:

In[36]:=
%[Unevaluated[1 + 1]]
Out[36]=

Use Unevaluated to perform replacements prior to evaluating expr:

In[37]:=
ResourceFunction["ReplaceAt"][Unevaluated[1 + 2 + 3], x_ :> f[x], 2]
Out[37]=

Possible Issues (3) 

Repeated replacement parts may produce unexpected results if the structure of the expression is expected to change during replacements:

In[38]:=
ResourceFunction["ReplaceAt"][{a, a}, _ -> Nothing, {{1}, {1}}]
Out[38]=

Compare to using Nest:

In[39]:=
Nest[ResourceFunction["ReplaceAt"][_ -> Nothing, 1], {a, a}, 2]
Out[39]=

This behavior is consistent with ReplacePart and MapAt:

In[40]:=
ReplacePart[{a, a}, {{1} -> Nothing, {1} -> Nothing}]
Out[40]=
In[41]:=
MapAt[Nothing &, {a, a}, {{1}, {1}}]
Out[41]=

Neat Examples (1) 

Modify the resource functions in a piece of code before running it:

In[42]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/47b6cb4d-becf-4c73-ae15-62f8daf47ac7"]
Out[43]=

Version History

  • 1.0.0 – 22 August 2019

Related Resources

License Information