is list comprehension necessary?

Gary Herron gherron at islandtraining.com
Wed Oct 27 00:36:17 EDT 2010


On 10/26/2010 09:28 PM, rantingrick wrote:
> On Oct 26, 12:07 pm, Andre Alexander Bell<p... at andre-bell.de>  wrote:
>> Hello,
>>
>> I occasionally use LCs, if they seem useful. However, what I don't like
>> about LCs is that they 'look-like' being a closed scope, while actually
>> they are in the scope of there call. Example:
>>
>>>>> i = 5
>>>>> l = [i**2 for i in range(3)]
>>>>> i
>> 2


That (very small) issue has been fixed in Python3:

 >>> l = [i**2 for i in range(3)]
 >>> i
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined



Gary Herron



> I must admit you make a good point here however the only time that
> will slip you up is when you first experienced the Python LC syntax.
> After a few "hello world" LC's you'll begin to love and understand
> them completely.




More information about the Python-list mailing list