# How to replace identical positions in two different lists

Assume that I have two lists like :

list1={1,2,3,4,5}; list2={a,b,c,d,e};

I want to replace some elements from list1 into list2 and make a new list, NewList. For example, I want to replace the elements of list1 from position 1 and 5, with the elements of list2 with the same positions. The result will be :

NewList={1,b,c,d,5};

Is there any way to do that ?!

Another way, doing it all at once:

Newlist = list2;
Newlist[[{1,5}]] = list1[[{1,5}]];


Gives

Newlist
(* {1, b, c, d, 5} *)

• It's a nice and easy method! Thank you. – Shellp Mar 31 '14 at 11:27
list1 = {1, 2, 3, 4, 5};
list2 = {a, b, c, d, e};

NewList = list2;
NewList[[1]] = list1[[1]];
NewList[[5]] = list1[[5]];
NewList


{1, b, c, d, 5}

Newlist = ReplacePart[#2, Rule @@@ Transpose[{#3, #1[[#3]]}]] &[list1, list2, {1, 5}]