# Setting the elements of a list equal to elements of a different list

A = {a,b,c,d,e,f,g,h}
B = {1,2,3,4,5}


How would I go about setting some of the elements in list A equal to the elements in list B and then the left over elements in list A will be set to 0. For example, set a=1, c=2, d=3, g=4, h=5 and then the leftover elements in list A are b,e,f=0. So the new list A would be

A = {1,0,2,3,0,0,4,5}


You didn't specify what your input should be so I have to guess. Perhaps:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
pos = {1, 3, 4, 7, 8};

A[[All]] = 0;
A[[pos]] = B;

A

{1, 0, 2, 3, 0, 0, 4, 5}


A = ConstantArray[0, Length @ A]

• I think that does not answer the right question: the OP wants to assign values to the symbols appearing in A. Sep 18 '14 at 16:50
• @IgorRivin my interpretation is that he was merely using that notation to describe the replacements. If not you win. :-) Sep 18 '14 at 18:50
 const = ConstantArray[0, Length[A]]
const[[pos]] = B

• Why a downvote? Sep 18 '14 at 17:45
• It wasn't me that downvoted, but I would guess it's because C is Protected, and C[pos] should have been C[[pos]]. Also Set by itself is cleaner than Set[#1, #2]& Sep 18 '14 at 17:54
• I too did not down-vote, but additionally this is making assignments to the symbols in the list A, which is unlike other interpretations. If that's actually what the OP wants then great. :-) Sep 18 '14 at 18:47
• @SimonWoods Fixed. Sep 18 '14 at 19:21

Just for fun:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
p = {1, 3, 4, 7, 8};


Then:

(A = ConstantArray[0, Length @ A];
Scan[(A[[p[[#]]]] = B[[#]]) &, Range @ Length @ pos]; A)

{1, 0, 2, 3, 0, 0, 4, 5}

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
pos = {1, 3, 4, 7, 8};

aA = SparseArray[pos -> B];
aA // Normal
(* {1,0,2,3,0,0,4,5} *)


More generally,

(* replace positions # of #3 with #2 and the rest with #4 *)
foo = SparseArray[# -> #2, Length @ #3, #4]&
foo[pos, B, A, 0] //Normal
(* {1, 0, 2, 3, 0, 0, 4, 5} *)
foo[pos, B, A, 100] // Normal (* use 100 instead of 0 as the default element *)
(* {1, 100, 2, 3, 100, 100, 4, 5} *)
foo[{2, 3, 4, 7}, {1, 2, 3, 4}, A, 100] // Normal (* replace any part of A *)
(* {100, 1, 2, 3, 100, 100, 4, 100} *)


Update: Few more alternatives:

ReplacePart[0 A, Thread[pos -> B]]
(* {1, 0, 2, 3, 0, 0, 4, 5} *)
Block[{i = 1}, MapAt[B[[i++]] &, 0 A, List /@ pos]]
(* {1, 0, 2, 3, 0, 0, 4, 5} *)

• As written this is very clean, but if the default element is not 0, and if you are not replacing the last element in A, you will need additional parameters, e.g. SparseArray[pos -> B, Length @ A, bg] where bg is the default. +1 but I suggest you include these caveats in the answer. Sep 18 '14 at 18:49
• @Mr.W, good point; thank you. Updated with your suggestions.
– kglr
Sep 18 '14 at 19:23

Also:

A = {a, b, c, d, e, f, g, h};
B = {1, 2, 3, 4, 5};
Replace[A, {a -> 2, b -> 5, _ -> 0}, 1]

(* {2, 5, 0, 0, 0, 0, 0, 0} *)

A = {a, b, c, d, e, f, g, h} ;
{a, c, d, g, h} = {1, 2, 3, 4, 5};
Replace[A, _Symbol :> 0, {-1}]
(*{1, 0, 2, 3, 0, 0, 4, 5}*)