On Tue, Sep 16, 2014 at 6:49 PM, Thomas Rachel
wrote:
> Am 13.09.2014 09:22 schrieb Chris Angelico:
>
>> In that case, don't iterate over the list at all. Do something like this:
>>
>> while lst:
>> element = lst.pop(0)
>> # work with element
>> lst.append(new_element)
>
>
> And if
Am 13.09.2014 09:22 schrieb Chris Angelico:
In that case, don't iterate over the list at all. Do something like this:
while lst:
element = lst.pop(0)
# work with element
lst.append(new_element)
And if you don't like that, define a
def iter_pop(lst):
while lst:
yiel
On Sun, Sep 14, 2014 at 1:27 AM, Ian Kelly wrote:
> On Sat, Sep 13, 2014 at 1:39 AM, Michael Welle wrote:
>>> In that case, don't iterate over the list at all. Do something like this:
>>>
>>> while lst:
>>> element = lst.pop(0)
>>> # work with element
>>> lst.append(new_element)
>>>
>
On Sat, Sep 13, 2014 at 1:39 AM, Michael Welle wrote:
>> In that case, don't iterate over the list at all. Do something like this:
>>
>> while lst:
>> element = lst.pop(0)
>> # work with element
>> lst.append(new_element)
>>
>> There's no mutation-while-iterating here, and it's clear t
Michael Welle wrote:
> I want to create an iterator it=iter(list) and control a for-loop with
> it. Is it save to append elements to the list in the body of the
> for-loop or is the behaviour undefined then? PEP234 notes that once the
> iterator has signaled exhaustion, subsequent calls of next()
On Sat, Sep 13, 2014 at 5:01 PM, Michael Welle wrote:
> ideed, this works for the minimal example. In a real application list
> comprehension might be a bit unhandy, because there is a lot of code
> involved.
Sure. Sometimes, cutting something down for posting makes a completely
different solutio
On Sat, Sep 13, 2014 at 4:09 PM, Michael Welle wrote:
> foo = [1,2,3,4]
> it = iter(foo)
>
> for e in it:
> if e % 2 == 0:
> x.append(e)
A better way to do this is with a list comprehension:
x = [e for e in foo if e %2 == 0]
Modifying something that you're iterating over is unspecif