removing common elemets in a list

Carsten Haese carsten at uniqsys.com
Wed May 16 09:18:18 EDT 2007


On Tue, 2007-05-15 at 23:17 -0700, 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.
>                                                       Thanks

If the list is sorted, you can weed out the duplicates with
itertools.groupby:

>>> import itertools
>>> L = [1,2,3,3,4,4,5]
>>> [k for (k,_) in itertools.groupby(L, lambda x:x)]
[1, 2, 3, 4, 5]

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list