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

Chris F.A. Johnson cfajohnson at gmail.com
Tue Dec 6 17:10:51 CET 2005


On 2005-12-06, Steve Holden wrote:
> Daniel Schüle wrote:
>> hi,
>> 
>> [...]
>> 
>> 
>>>># pseudo code
>>>>i=2
>>>>lst=[i**=2 while i<1000]
>>>>
>>>>of course this could be easily rewritten into
>>>>i=2
>>>>lst=[]
>>>>while i<1000:
>>>>    i**=2
>>>>    lst.append(i)
>>>>
>>>
>>>
>>>Neither of these loops would terminate until memory is exhausted. Do you 
>>>have a use case for a 'while' in a list comprehension which would 
>>>terminate?
>> 
>> 
>> unless I am missing something obvious, I can not see why the loop should 
>> not terminate
>
> In that case, kindly explain how the condition i<1000 can become false 
> when it starts at 2 and never changes! [In other words: you *are* 
> missing something obvious].

    What does i**=2 do if not change i?

>>> i=2
>>> lst=[]
>>> while i<1000:
...     i**=2
...     lst.append(i)
... 
>>> lst
[4, 16, 256, 65536]


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence



More information about the Python-list mailing list