Misuse of list comprehensions?
Diez B. Roggisch
deets at nospam.web.de
Tue May 20 10:38:08 EDT 2008
Thomas Bellman wrote:
> "Diez B. Roggisch" <deets at nospam.web.de> writes:
>
>> bearophileHUGS at lycos.com wrote:
>
>>> The Example 2 builds a list, that is then thrown away. It's just a
>>> waste of memory (and time).
>
>> No, it doesn't. It uses append because it refers to itself in the
>> if-expression. So the append(c) is needed - and thus the assignment
>> possible but essentially useless.
>
> Yes it does. A list comprehension *always* creates a list. In
> this case it will be a list of None, since that is what list.append()
> returns. See this:
Yep - no idea how that slipped me. I still don't mind the occasional waste
of a list-creation over a more concise looping-construct, but I totally
admit that one has to be aware of this. more than I was....
Diez
More information about the Python-list
mailing list