removing common elemets in a list
Gary Herron
gherron at islandtraining.com
Wed May 16 02:33:18 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.
> Thanks
>
>
Several ways, but probably not as efficient as using a set. (And why
don't you want to use a set, one wonders???)
>>> l = [1,2,3,1,2,1]
Using a set:
>>> set(l)
set([1, 2, 3])
Building the list element by element:
>>> for e in l:
... if e not in r:
... r.append(e)
...
>>> print r
[1, 2, 3]
Using a dictionary:
>>> d = dict(zip(l,l))
>>> d
{1: 1, 2: 2, 3: 3}
>>> d.keys()
[1, 2, 3]
>>>
More information about the Python-list
mailing list