Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List element replacement.

  • To: mathgroup at
  • Subject: [mg52011] Re: [mg51980] List element replacement.
  • From: Tomas Garza <tgarza01 at>
  • Date: Sun, 7 Nov 2004 01:03:28 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

Are the three lists the same Length? If that should be the case, e.g.,

initial = {0, 3, 4, 6, 8};
compare = {0, 0, 0, 3, 8};
replace = {0, 0, 0, 1, 7};

then the test whether "initial" has to change is based on MemberQ. All three 
elements of the test are True, and only in this case the replacements apply:

(MemberQ[initial, #1] & ) /@ Union[compare]

The positions of the elements to be replaced in this case are

replacements = Flatten[(Position[initial, #1] & ) /@

So, in general, the procedure is as follows

If[Union[(MemberQ[initial, #1] & ) /@
       Union[compare]][[1]] == True,
   initial[[replacements]] = replace[[

That is,  "initial" has been changed to


Tomas Garza
Mexico City
----- Original Message ----- 
From: "Robert G. Wilson v" <rgwv at>
To: mathgroup at
Subject: [mg52011] [mg51980] List element replacement.

> Et al,
> HELP, I tried this once before but what I received back I could not make 
> it work.
> Therefore I am following the adage that if at first you do not succeed 
> then wait
> awhile, rethink, restate and resubmit.
> I have three lists of objects, sorted but not unioned, that is there are 
> elements
> which will be repeated. Let us label them 'initial' list, 'compare' list, 
> and
> 'replace' list. If all of the elements in the 'compare' list, including 
> repeats,
> are in the 'initial' list, then remove those elements and put in the 
> elements from
> the 'replace' list.
> Thank you in advance,
> Bob.

  • Prev by Date: Re: Adding Vectors -- Newbie help please
  • Next by Date: Re: dashing the lines make the plot looks so bad
  • Previous by thread: List element replacement.
  • Next by thread: Re: List element replacement.