Question on lists

Heiko Wundram heikowu at ceosg.de
Wed Jul 28 00:24:56 EDT 2004


Am Mittwoch, 28. Juli 2004 05:26 schrieb Kristofer Pettijohn:
> My question is about lists:
>
> Is there a way to remove duplicate items from a list that are
> next to each other?

They don't even have to be next to each other:

(>= Python 2.3 required)

>>> import sets
>>> list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))
['a', 'c', 'b', 'd', 'ladida']

This doesn't maintain the order of the items, though. If you need that, and 
you're sure your list is always sorted, use the example Andrew gave.

Heiko.



More information about the Python-list mailing list