When is List Comprehension inappropriate?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Tue Mar 20 04:55:06 CET 2007


On Mon, 19 Mar 2007 07:41:59 -0700, Ben wrote:

> I have recently learned how list comprehension works and am finding it
> extremely cool.  I am worried, however, that I may be stuffing it into
> places that it does not belong.

Others have suggested reasons why you might or might not want to use list
comprehensions. Here's three more reasons:

* You have a LOT of data to handle. (But remember that a lot to you might
not be a lot to your computer.)

* You don't need to handle the items all at once, and can handle the items
one at a time instead.

Use an iterator, generator expression, or other lazily-evaluated function
instead. That way you avoid forming the list all at once.

* You have to write code that's backwards-compatible to an old version of
Python.

Use a list and a for-loop.



-- 
Steven.




More information about the Python-list mailing list