When is List Comprehension inappropriate?
Steve Holden
steve at holdenweb.com
Tue Mar 20 06:22:37 EDT 2007
Alex Martelli wrote:
> BJörn Lindqvist <bjourne at gmail.com> wrote:
> ...
>> even2 = [(pos, col) for pos, col in iterimage(im, width, height, 2)]
>
> list(iterimage(etc etc))
>
> is surely a better way to express identical semantics. More generally,
> [x for x in whatever] (whether x is a single name or gets peculiarly
> unpacked and repacked like here) is a good example of inappropriate LC,
> to get back to the question in the subject: list(whatever) is the "one
> obvious way" to perform the same task.
>
Clearly the comprehension you complain about is sub-optimal.
The essential difference, however, is between
[x for x in iterimage(im, width, height, 2)]
and
list(iterimage(im, width, height, 2))
I agree that the latter is the obvious way, but the difference isn't as
large as your leap makes it look - and we had to await the invention of
the generator expression for it to be a practical choice.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list
mailing list