else condition in list comprehension

Steven Bethard steven.bethard at gmail.com
Thu Jan 13 01:37:50 CET 2005


Steve Holden wrote:
> Nick Coghlan wrote:
>> z = [newval(i) for i in range(10)] using:
>>     def newval(x):
>>         if x % 2:
>>             return x - 2
>>         else:
>>             return x + 2
>>
>> Just some more mental twiddling relating to the thread on statement 
>> local namespaces.
>>
> I presume the point of this is to avoid polluting the local namespace 
> with "newval". I further presume you also have plans to do something 
> about "i"? ;-)

Well, while I'm not at all a fan of the "using" syntax, getting rid of 
'i' is simple:

z = list(newval(i) for i in range(10))

=)

Steve



More information about the Python-list mailing list