Algorithm help per favore

Anton Vredegoor anton at
Thu Jun 19 08:23:02 CEST 2003

Alex Martelli <aleax at> wrote:

>> [x for x, y in zip(L, [lambda x:x]+L) if x != y]
>> There must be a better way to do this than with a lambda.

<snip lambda>

>What about L[:1] + [x for x,y in zip(L[1:],L[:-1]) if x!=y] as
>a less-tricky variant of your list-comprehension?  Does need L
>to be a non-empty list (not another sequence, not an empty one),
>but that might be acceptable perhaps...

Nice idea, there seems to be no problem with L an empty list, but
there would be a problem with L a tuple. However that can be

   [x for x,y in zip(L,L[1:]+L[:-1]) if x!=y] or list(L[:1])


More information about the Python-list mailing list