Misuse of list comprehensions?

Hrvoje Niksic hniksic at xemacs.org
Tue May 20 10:14:19 EDT 2008


"Diez B. Roggisch" <deets at nospam.web.de> writes:

> bearophileHUGS at lycos.com wrote:
>
>> John Salerno:
>>> What does everyone think about this?
>> 
>> The Example 2 builds a list, that is then thrown away. It's just a
>> waste of memory (and time).
>
> No, it doesn't.

This line:

    [new.append(c) for c in s if c not in new]

...throws away the list built by the comprehension itself, composed of
None values (returned by append).

> It uses append because it refers to itself in the if-expression. So
> the append(c) is needed

The append is not in question here.



More information about the Python-list mailing list