list comprehension question

Steven D'Aprano steven at
Thu May 7 03:35:12 CEST 2009

On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote:

> Emile van Sebille <emile at> writes:
>> On 5/5/2009 9:15 AM J Kenneth King said...
>>> List comprehensions can make a reader of your code apprehensive
>>> because it can read like a run-on sentence and thus be difficult to
>>> parse. The Python documentation discourages their use and I believe
>>> for good reason.
>> Can you provide a link for this?  I'd like to see specifically what's
>> being discouraged, as I'd be surprised to find routine usage frowned
>> upon.
>> Emile
> "If you’ve got the stomach for it, list comprehensions can be nested.
> They are a powerful tool but – like all powerful tools – they need to be
> used carefully, if at all."

How does this discourage the use of list comprehensions? At most, it 
warns that complicated list comps are tricky. Complicated *anything* are 

> and
> "In real world, you should prefer builtin functions to complex flow
> statements."

That's ridiculous. The example given is a special case. That's like 
saying "Loops are hard, so in the real world, if you want a loop, find a 
builtin function that does what you want instead."

What's the "builtin function" we're supposed to prefer over a "complex 
flow statement" like this?

# split text into word fragments of length <= 3
sentence = "a sentence is a series of words"
new = [word[i:i+3] for word in sentence.split() for i in range(0, len(word), 3)]


More information about the Python-list mailing list