list comprehension question

Emile van Sebille emile at fenx.com
Wed May 6 17:04:44 EDT 2009


On 5/6/2009 6:48 AM J Kenneth King said...
> Emile van Sebille <emile at fenx.com> writes:
>> On 5/5/2009 9:15 AM J Kenneth King said...
>>>  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.
> http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions
> 

Thanks.

> 
> "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."

Yes, although this is in reference to nested list comprehensions.  Eg,

[xx for xx in [yy for yy in [zz for zz in iterable if z] if y] if x]

The section above on list comprehensions advocates their use saying:

"List comprehensions provide a concise way to create lists without 
resorting to use of map(), filter() and/or lambda. The resulting list 
definition tends often to be clearer than lists built using those 
constructs."

Emile




More information about the Python-list mailing list