removing common elemets in a list

Tim Golden mail at timgolden.me.uk
Wed May 16 02:32:43 EDT 2007


saif.shakeel at gmail.com wrote:
> Hi,
>      Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.

Is this a test? Why don't you want to use the set operator?
Anyway, you can just move things from one list into another
excluding those which are already moved:

<code>
numbers = [1, 2, 3, 3, 4, 4, 5]
unique_numbers = []
for n in numbers:
   if n not in unique_numbers:
     unique_numbers.append (n)

print unique_numbers
</code>

It won't be the fastest thing you could do, but it
does work. Using a dictionary would speed things up,
but then you're basically implementing a set using
a dictionary.

TJG



More information about the Python-list mailing list