# Part Assignment of variables with argument

I do not really understand why this:

a = {1, 2, 3};
a[] = 3;
a


{3,2,3}

works as desired, but this does not:

b = {1, 2, 3};
b[] = 3;
b


Set::setps: b in the part assignment is not a symbol.

Can I not treat variables with arguments like 'normal' variables?

• closely related: 148387 – Kuba Oct 22 '19 at 9:20

You can use ReplacePart:

ClearAll[b]
b = {1, 2, 3};
b = ReplacePart[ b, 1 -> 3];
b


{3, 2, 3}

Alternatively, MapAt:

ClearAll[b]
b = {1, 2, 3};
b = MapAt[3 &, b, {1}];
b


{3, 2, 3}

• Interesting thank you. What would I do If I wanted to set a column in a two Dimensional array? Like this: a = ConstantArray[1, {10, 10}] a[[;; , 3]] = ConstantArray[2, 10]; a – Mr Puh Oct 22 '19 at 9:33
• @MrPuh, the code in your comment works as expected. – kglr Oct 22 '19 at 9:35
• yeah, I mean in the sense of the question, I would want something like this now: a = ConstantArray[1, {10, 10}] a[[;; , 3]] = ConstantArray[2, 10]; a – Mr Puh Oct 22 '19 at 9:43
• @MrPuh, you can do ClearAll[a];a = ConstantArray[1, {10, 10}] ;a=MapAt[ 2&,a, {All,3}];a or ClearAll[a];a = ConstantArray[1, {10, 10}] ; a=ReplacePart[ a, {_,3}->2];a – kglr Oct 22 '19 at 9:53
• ... you can also use SubsetMap: ClearAll[a];a = ConstantArray[1, {10, 10}] ; a=SubsetMap[ConstantArray[2,10]&,a, {All,3}];a – kglr Oct 22 '19 at 9:55