Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?!

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Another way, doing it all at once:

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

Gives

Newlist
   (* {1, b, c, d, 5} *)
share|improve this answer
    
It's a nice and easy method! Thank you. –  Shellp Mar 31 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}

share|improve this answer
Newlist = ReplacePart[#2, Rule @@@ Transpose[{#3, #1[[#3]]}]] &[list1, list2, {1, 5}]
share|improve this answer
    
Thank you !! That's great. –  Shellp Mar 31 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.