Compress a string

John Salerno johnjsal at NOSPAMgmail.com
Tue May 20 00:38:57 EDT 2008


On Tue, 20 May 2008 00:09:14 -0400
John Salerno <johnjsal at NOSPAMgmail.com> wrote:

> Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.

Whoops! Could be cleaner! :)

def compress(s):
    new = []

    for c in s:
        if c not in new:
            new.append(c)
    return ''.join(new)


No, wait! I can do better!

def compress(s):
    new = []
    [new.append(c) for c in s if c not in new]
    return ''.join(new)

Wow, list comprehensions are cool.



More information about the Python-list mailing list