List Comprehension Syntax

Dave Brueck dave at
Sat Jul 10 22:20:57 CEST 2004

Moosebumps wrote:

>>What I like about LCs is that they made sense to me before reading the
>>documentation - they just came across as very expressive of what was
>>happening. I use the simplest forms, i.e.
>>[op(x) for x in y]
>>[x for x in y if z]
>>(as well as some slight variations)
> That's true, I tend to use the simplest forms too -- but then that is not as
> maintainable.
> We all know that things don't always stay so simple.  There is always
> something you need to add.  IMO with that in mind, it is easier to keep
> everything consistent, then to try to "sneak in" the LC for the simpler
> cases.  You would end up flipping back and forth too much when you need to
> add a condition, or add an expression.

I can see the potential for a maintenance problem, but I haven't seen it 
actually be a problem in practice.

Perhaps it's due in part to the fact that an LC normally wouldn't "grow" 
more complex in isolation - the change would likely be tied to changes 
in surrounding code as well - and so maybe the entire code block would 
be refactored. Dunno... but compared to the number of times LCs are used 
in code, the number of times they are later "unrolled" to the for-loop 
form has been too low to worry about.


More information about the Python-list mailing list