ReplacePart
ReplacePart(expr, i -> new)replaces part
iinexprwithnew.
ReplacePart(expr, {{i, j} -> e1, {k, l} -> e2})'replaces parts
iandjwithe1, and partskandlwithe2.
Examples
>> ReplacePart({a, b, c}, 1 -> t){t,b,c}
>> ReplacePart({{a, b}, {c, d}}, {2, 1} -> t){{a,b},{t,d}}
>> ReplacePart({{a, b}, {c, d}}, {{2, 1} -> t, {1, 1} -> t}){{t,b},{t,d}}
>> ReplacePart({a, b, c}, {{1}, {2}} -> t){t,t,c}Delayed rules are evaluated once for each replacement:
>> n = 1>> ReplacePart({a, b, c, d}, {{1}, {3}} :> n++){1,b,2,d}Non-existing parts are simply ignored:
>> ReplacePart({a, b, c}, 4 -> t){a,b,c}You can replace heads by replacing part 0:
>> ReplacePart({a, b, c}, 0 -> Times)a*b*cNegative part numbers count from the end:
>> ReplacePart({a, b, c}, -1 -> t){a,b,t}Related terms
Replace, ReplaceAll, ReplaceList, ReplaceRepeated
Implementation status
- ✅ - full supported