Algorithm help per favore

Alex Martelli aleax at
Wed Jun 18 21:57:51 CEST 2003

David Eppstein wrote:

> In article <3EF0AAFB.DDA5F555 at>,
>  Peter Hansen <peter at> wrote:
>> > >>> L = [6,3,3,3,5,7,6,3,4,4,3]
>> > >>> [x for x, y in zip(L, [L]+L) if x != y]
>> > [6, 3, 5, 7, 6, 3, 4, 3]
>> But this won't work if L contains a reference to L!  :-) :-)
> Ok, what's the quickest way to build a new object that's guaranteed
> unequal to any previous object?  E.g.
> [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.

Actually, "lambda:0" is a good candidate for "an object that's
not gonna equal any object with a different id()" -- it's just
8 characters.  "object()" is also 8 characters.  Not sure what
you mean by "quickest" or "better", but number of characters in
the source seems an objective-enough measurement.

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...


More information about the Python-list mailing list