i=2; lst=[i**=2 while i<1000]

Daniel Schüle uval at rz.uni-karlsruhe.de
Tue Dec 6 09:17:27 EST 2005


D H wrote:
> bonono at gmail.com wrote:
> 
>>> You can use i**=2 for i in range(1000) instead
>>
>>
>>
>> I don't think one can use assignment in list comprehension or generator
>> expression. The limitation is very much like lambda.
>>
> 
> i**2

lst=[i**2 for i in range(1000)]

you will get a list with 1000 items
[0,1,4,9 ... ]

is not the same as

i,lst=2,[]
while i<1000:
	i**=2
	lst.append(i)

here you get [4,16,256,65536]
only 4 items

Regards, Daniel




More information about the Python-list mailing list