list comprehensions *are* nice

Grant Griffin not.this at seebelow.org
Fri May 18 13:44:21 EDT 2001


In article <9doc1h$3sp$1 at slb3.atl.mindspring.net>, "Andrew says...
>
>A couple months ago I complained that list comprehensions were
>too confusing and that I would still be using
>
>   results = []
>   for val in data:
>     results.append(..some.expression.using.val..)
>
>I was wrong.
...

I was cold to them at first, too, but I warmed up to them.

Then I got a little cooler again.  One problem, of course, is a lack of
compatability with 1.5.2.  But a _bigger_ problem for me (even in 2.0+) has been
similar to the problem I have with the following construct in C:

    if (x)
        do_something();

Many years ago, I developed the habbit of writing that as:

    if (x) {
        do_something();
    }

because I found I usually had to do add more too it later:

    if (x) {
        do_something();
        do_something_else();
    }

Also, of course, the latter form tends to reduce mistakes.

OK, the comparison to list comprehensions is a stretch, but my point here (if
any <wink>) is that I've found that I often undergo extra effort by having to
rewrite them "the old-fashioned way" after the stuff inside the loop has grown
in complexity beyond what list comprehensions can handle; in such cases, I would
have been better off to have written them the old-fashioned way in the first
place.

Therefore, I have learned to use list comprehensions selectively.  They're great
for some things, but not so great for other things--even things they can well
do.  (In that vein, I even used "filter" for the first time the other day!)

That being said, I do kindda admire the way list comprehensions tend to stetch
across 80 characters, enabling you to use a "\" to continue them on the next
line <wink>.

anything-that's-more-complicated-than-complex-and-more-ugly-than
  -beautiful-can't-be-all-good

    or

if-only-list-comprehensions-had-a-'break'-statement-<wink>

-ly y'rs,

=g2

_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation            http://www.iowegian.com




More information about the Python-list mailing list