Replace

Replace

Replace[ list, new, positions ] replaces the elements at the given positions of a list.

Usages

Replace[ list, {new1, new2, ...}, {i1, i2, ...} ]

Returns a copy of list in which the element at position i1 is replaced by new1, the element at position i2 by new2, and so on. Both lists must have the same length.

Example in out
Replace the second element. Replace[ {a, b, c}, {b2}, {2} ] {a, b2, c}
Replace the first and third element. Replace[ {a, b, c}, {x, z}, {1, 3} ] {x, b, z}
Replace a term of a sum. Replace[ 2x + 3y, {5z}, {2} ] 2x+5z

Replace[ list, x -> expr, {i1, i2, ...} ]

With a mapping, the mapping is applied to the elements at the given positions.

Example in out
Square the second element. Replace[ {a, b, c}, x -> x^2, {2} ] {a, b^2, c}
Multiply the first and third element by 10. Replace[ {1, 2, 3}, x -> CalcTimes[x, 10], {1, 3} ] {10, 2, 30}
Details
Invalid input Result
Replace[ {a, b, c}, {x, y}, {2} ] Error: the length of the 2nd and 3rd arguments should be equal
Replace[ {a, b, c}, {x}, {5} ] Error: invalid position
Replace[ {a, b, c}, {x} ] Error: the number of arguments should be three

See also: RemoveI, Substitute, Map, Elm ([[ ]])