This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / New in Version 3.0 / Lists and Matrices  /

  • ReplacePart[ expr , new , n ] yields an expression in which the n part of expr is replaced by new.
  • ReplacePart[ expr , new , i , j , ... ] replaces the part at position i , j , ... .
  • ReplacePart[ expr , new , , , ... , , , ... , ... ] replaces parts at several positions by new.
  • ReplacePart[ expr , new , pos , npos ] replaces parts at positions pos in expr by parts at positions npos in new.
  • Example: ReplacePart[ a, b, c, d , x, 3].
  • The list of positions used by ReplacePart is in the same form as is returned by the function Position.
  • ReplacePart[ expr , Hold[ new ], pos , 1] can be used to replace a part without evaluating it.
  • If pos and npos both specify multiple parts, each part in pos is replaced by the corresponding part in npos.
  • See the Mathematica book: Section 1.8.6Section 2.1.4.
  • See also: Part, Extract, MapAt, FlattenAt, Insert, Delete, Sequence, StringReplacePart.

    Further Examples

    This puts x into the third part of the expression.



    A powerful application of ReplacePart is in the partial evaluation of a held expression. Here's a function that takes an expression and a list of positions, and evaluates in place the parts at the specified positions.


    In this case, {1,2} specifies the second element of the held list, and {1,-1,1} the first part of the last element.