list comprehension question

J Kenneth King james at agentultra.com
Tue May 5 12:15:15 EDT 2009


Emile van Sebille <emile at fenx.com> writes:

> On 5/1/2009 7:31 AM J Kenneth King said...
>> Chris Rebert <clp2 at rebertia.com> writes:
>>> b = []
>>> for pair in a:
>>>     for item in pair:
>>>         b.append(item)
>>
>> This is much more clear than a nested comprehension.
>>
>> I love comprehensions, but abusing them can lead to really dense and
>> difficult to read code.
>
> I disagree on dense and difficult, although I'll leave open the
> question of abuse.

Dense and difficult may be subjective to people like you or I, but you
left out the "to read" part of that sentence. I was referring to the
negative effect nested or complex list comprehensions can have on
readability.

> b = [ item for pair in a for item in pair ]

It's a clever statement, but use it once and its gone. Why not use the
expanded form in a function definition and get an even shorter, but
clearer statement?

>>> b = flatten(a)

Boom, done.

> This is exactly the code above expressed in comprehension form.
>
> It's worth knowing that a list comprehension is structured identically
> to the equivalent for loop.  So it really is neither more dense nor
> more difficult to read.  Further, you can tell immediately from the
> start of the list comprehension what you've got -- in this case a list
> of item(s).
>
> Here with some slight changes...
>
>>>> a = [(1, 2), (3, 4, 7), (5, 6)]
>>>> [ item for j in a if len(j)==2 for item in j if item % 2 ]
> [1, 5]
>
> ...opposed to...
>
>>>> for j in a:
> ...     if len(j)==2:
> ...         for item in j:
> ...             if item % 2:
> ...                 b.append(item)
> ...
>>>> b
> [1, 5]
>>>>

Thanks for the lesson in list comprehensions, but I'm well aware of
how they work.

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. It's much easier to explain complex processes with a
function rather than a single nested statement.

>
> YMMV,
>
> Emile

It will apparently vary greatly. Depending on how much coffee I've
had.

;)

J



More information about the Python-list mailing list