Yet another unique() function...

MonkeeSage MonkeeSage at gmail.com
Wed Feb 28 16:10:24 EST 2007


On Feb 28, 2:18 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Unicode fix (untested):
>
> def unique(seq, keepstr=True):
>     t = type(seq)
>     if t in (unicode, str):
>         t = (list, t('').join)[bool(keepstr)]
>     seen = []
>     return t(c for c in seq if not (c in seen or seen.append(c)))

This definitely works. I tried to post a message about this a few
hours ago, but I guess it didn't go through. I've already updated the
recipe and comments.

> I think these iterator approaches get more readable as one becomes
> used to them.

I agree. I understood your code in a matter of seconds.




More information about the Python-list mailing list