delete duplicates in list

Corrado Gioannini gioco at nekhem.com
Thu Oct 30 06:12:48 EST 2003


On Wed, Oct 29, 2003 at 10:02:08PM +0100, christof hoeke wrote:
> there should be an easier or more intuitive solution, maybe with a list 
> comprehension=
> 
> somthing like
> 
> >>> b = [x for x in a if x not in b]
> >>> print b
> []
> 
> does not work though.
if you want a list comp solution, similar to the one you proposed and valid
also with python < 2.3 you could do:

>>> a = [1, 2, 2, 3]
>>> b=[]
>>> [b.append(x) for x in a if x not in b]
[None, None, None]
>>> b
[1, 2, 3]

or even:

>>> a = [1, 2, 2, 3]
>>> [b.append(x) for b in [[]] for x in a if x not in b]
[None, None, None]
>>> b
[1, 2, 3]

Corrado.
-- 
                       Thought is only a flash between two long nights,
                                          but this flash is everything.
                                                          (H. Poincaré)






More information about the Python-list mailing list