Misuse of list comprehensions?

Delaney, Timothy (Tim) tdelaney at avaya.com
Tue May 27 21:09:05 EDT 2008


Ian Kelly wrote:

> It sounds like the wasteful list creation is the biggest objection to
> using a list comprehension.  I'm curious what people think of this
> alternative, which avoids populating the list by using a generator
> expression instead (apart from the fact that this is still quadratic,
> which I'm aware of).
> 
> def compress(s):
>    new = []
>    filter(None, (new.append(c) for c in s if c not in new))
>    return ''.join(new)

Are you aware that filter() returns a list populated from its arguments?

Tim Delaney



More information about the Python-list mailing list