Skip to content

ReplacePart

ReplacePart[e, {loc1 -> newval1, ...}] replaces the value at the locations with their corresponding new values in e.

Attributes[ReplacePart] := {Protected}

Simple examples

In[1]:= ReplacePart[Range[4], foo, 2]
Out[1]= {1, foo, 3, 4}
In[2]:= ReplacePart[Range[4], 3 -> foo]
Out[2]= {1, 2, foo, 4}
In[3]:= ReplacePart[Range[4], {3} -> foo]
Out[3]= {1, 2, foo, 4}
In[4]:= ReplacePart[Range[4], {3, 1} -> foo]
Out[4]= {1, 2, 3, 4}

Further examples

In[1]:= ReplacePart[Range[4], i_ -> foo]
Out[1]= {foo, foo, foo, foo}
In[2]:= ReplacePart[Range[4], 7 -> foo]
Out[2]= {1, 2, 3, 4}
In[3]:= ReplacePart[a + b^c, {2, 2} -> foo]
Out[3]= a + b^foo
In[4]:= ReplacePart[a + b^c, {2, 2, 1} -> foo]
Out[4]= a + b^c