Misuse of list comprehensions?
Paul McGuire
ptmcg at austin.rr.com
Tue May 20 18:16:59 EDT 2008
On May 20, 11:08 am, Paul Hankin <paul.han... at gmail.com> wrote:
> On May 20, 3:58 pm, Paul McGuire <pt... at austin.rr.com> wrote:
>
> > def compress(s):
> > seen = set()
> > return ''.join(c for c in s if c not in seen and (seen.add(c) or
> > True))
>
> Slightly nicer is to move the set add out of the conditional...
>
> def compress(s):
> seen = set()
> return ''.join(seen.add(c) or c for c in s if c not in seen)
>
> I wouldn't write it this way though :)
>
> --
> Paul Hankin
Thank you, Paul. I knew I had seen a cleaner version than mine, and I
could not remember how it was done.
-- Paul
More information about the Python-list
mailing list