list comprehension question

J Kenneth King james at agentultra.com
Fri May 8 10:21:12 EDT 2009


Terry Reedy <tjreedy at udel.edu> writes:

> J Kenneth King wrote:
>>
>> Keep in mind that nested comprehensions are still available because
>> they do have a use case that justifies their existence.
>
> Nested comprehensions are available because because the syntax makes
> them available by default and making a fiddly exception would be
> contrary to Python's style.  A list comp creates an iterable. A list
> comp requires use of an iterable.  Therefore, a list comp may use a
> list comp.
>
>> However, I
>> think the Python documentation warns against their use because people
>> might rely on them for problems where they aren't necessary and since
>> they are difficult to read... it can lead to difficult to read code.
>
> Whenever the expression that results in the iterable used by a list
> comp is sufficiently complex, readability is improved by pulling it
> out as a separate statement. Nested list comps are often examplex of
> such sufficiently complex expressions, but not the only possible one.
>
> tjr

Agreed. A very succinct explanation of the point I was trying to make.



More information about the Python-list mailing list