list comprehension

Terry Hancock hancock at anansispaceworks.com
Mon Jul 3 23:17:52 EDT 2006


Andy Dingley <dingbat at codesmiths.com> wrote:
>  Simon Forman wrote:
>
> > There's more to it, but that's the basic idea.
>
>  This much I knew, but _why_ and _when_ would I choose to use list
>  comprehension (for good Python style), rather than using a simple
>  "traditional" loop ?

They are a very compact way of constructing a modified list. I also
find them clearer than the older way of doing "functional" filtering.

So, for example, what if I had a set of objects which all use a 'name'
attribute for some kind of tagging.  I could collect all the names in
a list comprehension:

names = [ob.name for ob in my_objects_list]

or only the names of the ones which also have a 'portal' attribute:

names = [ob.name for ob in my_objects_list if hasattr(ob, 'portal')]

or get the names in "all caps":

names = [ob.name.upper() for ob in my_objects_list]

without cluttering up my code with many lines of loop processing
code that doesn't really do much of significance.  I.e. if it's just routine
transformations, then list comps are good style.

Complex and clever looping and manipulation should probably
be spelled out instead, so the reader can follow in more detail
what you're doing.  List comps are a useful short hand when you
want to say "Oh, and just capitalize all the results so they look
prettier on output" (please no aesthetics comments on the
capitalization, it's just a dumb example).

>  If I want to generate something that's simply ( [1] + [2] + [3]+... )
>  then list comprehension is obviously the tool of choice. I suspect
>  though that there's more to it than this. Is list comprehension also
>  treatable as a sneaky concise formulation for nested lists, where
>  they're as much about selection of individual elements, so much as
>  concatenation of the sequence?

IMHO, nested list comps are pretty hard to read. Handle with care.

>  What happens if a comprehension has side effects, such as from
>  calling a function within it? Is this regarded as good or bad coding
>  style? Is it evil (as structured programming would claim) or is it a
>  concise formulation for an iterator or visitor pattern ?

My opinion is that it's fairly evil.  I'd spell out anything complicated
or tricky.

Remember that a list-comp doesn't really save you that much space,
and that if you have to split the list comp over more than one line,
that it becomes even less readable.

I think the thing about list comps is that they are easy to ignore or
skip over as less important elements of the code. So if you hide
something in a list comp, it's a bit like hiding stuff in the fine print
on a legal document. Or putting something only in an equation or
figure of a scientific paper without covering it in the main flow of
the text.  List comps are for incidental stuff that's intrinsically
obvious in meaning (or about which the reader needn't be concerned
with the meaning).

One nice thing about them, as one-liners: you can paste a list
comp into an interactive python session, and -- if you've set up
some test data -- you can just see what it does by trying it.

Some people have even made clever obfuscated email signatures
this way.

Cheers,
Terry


-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list