list comprehension question
Terry Reedy
tjreedy at udel.edu
Thu May 7 16:10:03 EDT 2009
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
More information about the Python-list
mailing list