WolframLang: Change Element in List
Replace Parts by Position
ReplacePart
-
Replace parts by Position .
(* replace second element by x *) ReplacePart[ {a, b, c}, 2 -> x ] === {a, x, c} (* replace second element by x *) ReplacePart[ {a, {3, 4}, c}, 2 -> x ] === {a, x, c} (* replace multiple positions *) ReplacePart[ {a, b, c, d}, {2 -> x, 3 -> y} ] === {a, x, y, d} (* replace element at position {2,1} by x *) ReplacePart[ {a, {3, 4}, c}, {2,1} -> x ] === {a, {x, 4}, c}
Replace Parts by Position and Assignment
you can also modify a element by assignment.
var[[n]] = val
-
Assign a new value at index n in var. Note,
expr[[n]]
is shortcut forPart
xx = {a, b, c}; xx[[2]] = 4; xx === {a, 4, c}
Replace Parts by Pattern
WolframLang List Operations
- WolframLang: List Operations
- WolframLang: Get Parts of List
- WolframLang: Add Element to List
- WolframLang: Delete Element in List
- WolframLang: Change Element in List
- WolframLang: Check Item Existence in List
- WolframLang: List Order Functions
- WolframLang: List Restructure Functions
- WolframLang: List Combinatorics
- WolframLang: List Join, Union, Intersection, Difference