MathGroup Archive 2002

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

Search the Archive

Re: Keeping order with Union

  • To: mathgroup at
  • Subject: [mg32612] Re: [mg32590] Keeping order with Union
  • From: Tomas Garza <tgarza01 at>
  • Date: Thu, 31 Jan 2002 01:45:44 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

I guess this problem has been posed before in this forum, and many answers
must have been put forward. Anyway, being too lazy to search in the
archives, I propose the following. Let lst be your list, from which you wish
to eliminate duplicates, preserving the order. Define the function elimReps:

elimReps[u_, v_] := If[MemberQ[u, v], u, Append[u, v]]

Then use Fold on your list as follows:

Fold[elimReps, {First[lst]}, Rest[lst]]


list1={a, b, c, e, e, g, a, 1, 5, 1, b};

Fold[elimReps, {First[list1]}, Rest[list1]]
{a, b, c, e, g, 1, 5}

list2 = Table[Random[Integer, {0, 9}], {10}]
{0, 9, 8, 3, 0, 7, 3, 6, 4, 2}

Fold[elimReps, {First[list2]}, Rest[list2]]
{0, 9, 8, 3, 7, 6, 4, 2}

Tomas Garza
Mexico City

----- Original Message -----
From: "Dave Snead" <dsnead6 at>
To: mathgroup at
Subject: [mg32612] [mg32590] Keeping order with Union

> I'd like a function that throws out duplicate entries in a list and keeps
> the
> original list order.  (when it sees 2 equal entries it throws out the one
> that is later in the list).   I'd use Union, but it sometimes changes the
> order of the elements in the list.  What's the tersest way to do this?
> Thanks in advance,
> Dave Snead

  • Prev by Date: Re: memoizing function again
  • Next by Date: Re: Gross Bug in Simplify
  • Previous by thread: Keeping order with Union
  • Next by thread: Re: Keeping order with Union